From cf3bc4d06e125c91228bb486cbf38116a7015d26 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9=20P=C3=B6nitz?= Date: Wed, 10 Oct 2001 16:35:18 +0000 Subject: [PATCH] small fixes to bugs discoverd by Dekel git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2857 a592a061-630c-0410-9148-cb99ea01b6c8 --- src/mathed/formulabase.C | 2 +- src/mathed/math_cursor.C | 160 +++++++++++++++++++++++--------------- src/mathed/math_cursor.h | 4 + src/mathed/math_factory.C | 3 + 4 files changed, 105 insertions(+), 64 deletions(-) diff --git a/src/mathed/formulabase.C b/src/mathed/formulabase.C index 0318936287..4b3e34f500 100644 --- a/src/mathed/formulabase.C +++ b/src/mathed/formulabase.C @@ -309,7 +309,7 @@ UpdatableInset::RESULT InsetFormulaBase::localDispatch(BufferView * bv, kb_action action, string const & arg) { - //lyxerr << "InsetFormulaBase::LocalDispatch: act: " << action + //lyxerr << "InsetFormulaBase::localDispatch: act: " << action // << " arg: '" << arg << "' cursor: " << mathcursor << "\n"; if (!mathcursor) diff --git a/src/mathed/math_cursor.C b/src/mathed/math_cursor.C index f7bfc0e1b8..eaf975a597 100644 --- a/src/mathed/math_cursor.C +++ b/src/mathed/math_cursor.C @@ -472,7 +472,8 @@ void MathCursor::plainInsert(MathInset * p) if (inner()) { array().insert(pos(), p); ++pos(); - swap(prevAtom()->nucleus(), nextAtom()->nucleus()); + if (prevAtom() && nextAtom()) // should be unnecessary + swap(prevAtom()->nucleus(), nextAtom()->nucleus()); return; } @@ -514,7 +515,7 @@ void MathCursor::insert(MathInset * p) void MathCursor::niceInsert(MathInset * p) { if (!p) { - lyxerr << "should not happen\n"; + lyxerr << "MathCursor::niceInsert: should not happen\n"; return; } selCut(); @@ -581,8 +582,7 @@ void MathCursor::backspace() } if (pos() == 0) { - if (size()) - pullArg(false); + pullArg(false); return; } @@ -728,24 +728,30 @@ void MathCursor::macroModeClose() { string s = macroName(); if (s.size()) { - pos() = pos() - s.size() - 1; - for (unsigned i = 0; i <= s.size(); ++i) - plainErase(); - lastcode_ = LM_TC_VAR; - interpret("\\" + s); + size_type old = pos(); + pos() -= s.size(); + array().erase(pos(), old); + interpret(s); } } -string MathCursor::macroName() const +int MathCursor::macroNamePos() const { - string s; - for (int i = pos() - 1; i >= 0; --i) { + for (int i = pos() - 1; i >= 0; --i) { MathInset * p = array().at(i)->nucleus(); - if (!p || p->code() != LM_TC_TEX || p->getChar() == '\\') - break; - s = p->getChar() + s; + if (p && p->code() == LM_TC_TEX && p->getChar() == '\\') + return i; } + return -1; +} + + +string MathCursor::macroName() const +{ + string s; + for (int i = macroNamePos(); i >= 0 && i < int(pos()); ++i) + s += array().at(i)->nucleus()->getChar(); return s; } @@ -959,7 +965,7 @@ bool & MathCursor::inner() bool MathCursor::inMacroMode() const { - return lastcode_ == LM_TC_TEX; + return macroNamePos() != -1; } @@ -983,9 +989,25 @@ MathArrayInset * MathCursor::enclosingArray(MathCursor::idx_type & idx) const void MathCursor::pullArg(bool goright) { - // pullArg dump("pullarg"); MathArray a = array(); + + MathScriptInset const * p = par()->asScriptInset(); + if (p) { + // special handling for scripts + const bool up = p->up(); + popLeft(); + if (nextAtom()) { + if (up) + nextAtom()->removeUp(); + else + nextAtom()->removeDown(); + } + ++pos(); + array().insert(pos(), a); + return; + } + if (popLeft()) { plainErase(); array().insert(pos(), a); @@ -1337,17 +1359,20 @@ MathMatrixInset * MathCursor::outerPar() const void MathCursor::interpret(string const & s) { - //lyxerr << "interpret: '" << s << "'\n"; + //lyxerr << "interpret 1: '" << s << "'\n"; //lyxerr << "in: " << in_word_set(s) << " \n"; if (s.empty()) return; - char c = s[0]; + if (s.size() == 1) { + interpret(s[0]); + return; + } - //lyxerr << "char: '" << c << "' int: " << int(c) << endl; - //owner_->getIntl()->getTrans().TranslateAndInsert(c, lt); - //lyxerr << "trans: '" << c << "' int: " << int(c) << endl; + //lyxerr << "char: '" << s[0] << "' int: " << int(s[0]) << endl; + //owner_->getIntl()->getTrans().TranslateAndInsert(s[0], lt); + //lyxerr << "trans: '" << s[0] << "' int: " << int(s[0]) << endl; if (s.size() > 7 && s.substr(0, 7) == "matrix ") { unsigned int m = 1; @@ -1377,23 +1402,63 @@ void MathCursor::interpret(string const & s) return; } - if (s.size() > 1) { - niceInsert(createMathInset(s.substr(1))); - return; - } + niceInsert(createMathInset(s.substr(1))); +} + + +void MathCursor::interpret(char c) +{ + //lyxerr << "interpret 2: '" << c << "'\n"; + + if (inMacroMode()) { + string name = macroName(); + + if (name == "\\" && c == '#') { + insert(c, LM_TC_TEX); + return; + } + + if (name == "\\" && c == '\\') { + backspac/(); + interpret("\\backslash"); + return; + } + if (name == "\\#" && '1' <= c && c <= '9') { + insert(c, LM_TC_TEX); + macroModeClose(); + return; + } - // we got just a single char now + if (isalpha(c)) { + insert(c, LM_TC_TEX); + return; + } + + if (name == "\\") { + insert(c, LM_TC_TEX); + macroModeClose(); + return; + } + + macroModeClose(); + return; + } + // no macro mode if (c == '^' || c == '_') { - const bool up = (s[0] == '^'); + const bool up = (c == '^'); + const bool in = inner(); selCut(); - if (inner()) + if (in) ++pos(); if (!prevAtom()) insert(0); MathInset * par = prevAtom()->ensure(up); - pushRight(par); + if (in) + pushLeft(par); + else + pushRight(par); selPaste(); return; } @@ -1407,12 +1472,6 @@ void MathCursor::interpret(string const & s) } if (c == ' ') { - if (inMacroMode()) { - macroModeClose(); - lastcode_ = LM_TC_VAR; - return; - } - MathSpaceInset * p = prevSpaceInset(); if (p) { p->incSpace(); @@ -1429,41 +1488,17 @@ void MathCursor::interpret(string const & s) return; } - if (lastcode_ != LM_TC_TEX && strchr("{}", c)) { + if (strchr("{}", c)) { insert(c, LM_TC_TEX); return; } - if (lastcode_ != LM_TC_TEX && strchr("#$%", c)) { + if (strchr("#$%", c)) { insert(new MathSpecialCharInset(c)); lastcode_ = LM_TC_VAR; return; } - if (lastcode_ == LM_TC_TEX) { - if (macroName().empty()) { - insert(c, LM_TC_TEX); - if (!isalpha(c) && c != '#') { - macroModeClose(); - lastcode_ = LM_TC_VAR; - } - } else { - if ('1' <= c && c <= '9' && macroName() == "#") { - insert(c, LM_TC_TEX); - macroModeClose(); - lastcode_ = LM_TC_VAR; - } - else if (isalpha(c)) { - insert(c, LM_TC_TEX); - } - else { - macroModeClose(); - lastcode_ = LM_TC_VAR; - } - } - return; - } - if (isalpha(c) && (lastcode_ == LM_TC_GREEK || lastcode_ == LM_TC_GREEK1)) { static char const greekl[][26] = {"alpha", "beta", "chi", "delta", "epsilon", "phi", @@ -1494,7 +1529,6 @@ void MathCursor::interpret(string const & s) } if (c == '\\') { - lastcode_ = LM_TC_TEX; insert(c, LM_TC_TEX); //bv->owner()->message(_("TeX mode")); return; diff --git a/src/mathed/math_cursor.h b/src/mathed/math_cursor.h index b59c376894..d8d7a5b49b 100644 --- a/src/mathed/math_cursor.h +++ b/src/mathed/math_cursor.h @@ -141,6 +141,8 @@ public: /// void interpret(string const &); /// + void interpret(char); + /// void setSize(MathStyles); /// bool toggleLimits(); @@ -278,6 +280,8 @@ private: /// string macroName() const; /// + int macroNamePos() const; + /// void insert(char, MathTextCodes t); /// can we enter the inset? bool openable(MathInset *, bool selection) const; diff --git a/src/mathed/math_factory.C b/src/mathed/math_factory.C index 34b0ba5d36..7152b27159 100644 --- a/src/mathed/math_factory.C +++ b/src/mathed/math_factory.C @@ -70,6 +70,9 @@ MathInset * createMathInset(string const & s) if (s.size() == 2 && s[0] == '#' && s[1] >= '1' && s[1] <= '9') return new MathMacroArgument(s[1] - '0'); + if (s.size() == 3 && s[0] == '\\' && s[1] == '#' && s[2] >= '1' && s[2] <= '9') + return new MathMacroArgument(s[2] - '0'); + latexkeys const * l = in_word_set(s); if (l) return createMathInset(l); -- 2.39.2