]> git.lyx.org Git - features.git/commitdiff
small fixes to bugs discoverd by Dekel
authorAndré Pönitz <poenitz@gmx.net>
Wed, 10 Oct 2001 16:35:18 +0000 (16:35 +0000)
committerAndré Pönitz <poenitz@gmx.net>
Wed, 10 Oct 2001 16:35:18 +0000 (16:35 +0000)
git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@2857 a592a061-630c-0410-9148-cb99ea01b6c8

src/mathed/formulabase.C
src/mathed/math_cursor.C
src/mathed/math_cursor.h
src/mathed/math_factory.C

index 031893628706e4e139234de6e5eab1ce2f5eec2d..4b3e34f50053ef3d29e0a80a7624a9ddb951f2e9 100644 (file)
@@ -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) 
index f7bfc0e1b8fff9fb086065864e6489265367325e..eaf975a597321f2d1f0346f21b16dcba1e1e75ee 100644 (file)
@@ -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; 
index b59c376894384eeb0ae019c34f60706c6e4ea2ff..d8d7a5b49bd636cf14de260a1f9d9c3b84addd12 100644 (file)
@@ -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;
index 34b0ba5d3642f731d311c2dbc1e2a00f0670437f..7152b271594c89f392d7ba1f02eb76d6ce055b0c 100644 (file)
@@ -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);