]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/InsetMathNest.cpp
A little cleanup of the layout files.
[lyx.git] / src / mathed / InsetMathNest.cpp
index b4b0c0d3439b4e15de23e4518eed08625bea687e..c6e25b59945d0696f10b15e040631a79c724ae41 100644 (file)
 #include "InsetMathBig.h"
 #include "InsetMathBox.h"
 #include "InsetMathBrace.h"
+#include "InsetMathChar.h"
 #include "InsetMathColor.h"
 #include "InsetMathComment.h"
 #include "InsetMathDelim.h"
+#include "InsetMathEnsureMath.h"
 #include "InsetMathHull.h"
 #include "InsetMathRef.h"
 #include "InsetMathScript.h"
@@ -42,6 +44,7 @@
 #include "Cursor.h"
 #include "CutAndPaste.h"
 #include "DispatchResult.h"
+#include "Encoding.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LyXFunc.h"
@@ -514,7 +517,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
        //lyxerr << "InsetMathNest: request: " << cmd << endl;
 
-       Parse::flags parseflg = Parse::QUIET;
+       Parse::flags parseflg = Parse::QUIET | Parse::USETEXT;
 
        switch (cmd.action) {
 
@@ -814,8 +817,14 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                // InsetMathFrac -> a pos value > 0 is invalid.
                // A side effect is that an undo before the macro is finished
                // undoes the complete macro, not only the last character.
-               if (!cur.inMacroMode())
-                       cur.recordUndoSelection();
+               if (!cur.inMacroMode()) {
+                       MathMacro const * macro = 0;
+                       if (cur.pos() > 0 && cmd.argument() != "\\")
+                               macro = cur.inset().asInsetMath()->asMacro();
+                       
+                       if (!macro)
+                               cur.recordUndoSelection();
+               }
 
                // spacial handling of space. If we insert an inset
                // via macro mode, we want to put the cursor inside it
@@ -946,7 +955,10 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                docstring const save_selection = grabAndEraseSelection(cur);
                selClearOrDel(cur);
                //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
-               cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
+               if (currentMode() == Inset::TEXT_MODE)
+                       cur.plainInsert(MathAtom(new InsetMathEnsureMath));
+               else
+                       cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
                cur.posBackward();
                cur.pushBackward(*cur.nextInset());
                cur.niceInsert(save_selection);
@@ -1242,10 +1254,6 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
        case LFUN_FONT_DEFAULT:
                flag.setEnabled(true);
                break;
-       case LFUN_MATH_MUTATE:
-               //flag.setOnOff(mathcursor::formula()->hullType() == to_utf8(cmd.argument()));
-               flag.setOnOff(false);
-               break;
 
        // we just need to be in math mode to enable that
        case LFUN_MATH_SIZE:
@@ -1517,7 +1525,13 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                }
 
                // leave macro mode and try again if necessary
-               cur.macroModeClose();
+               if (cur.macroModeClose()) {
+                       MathAtom const atom = cur.prevAtom();
+                       if (atom->asNestInset() && atom->isActive()) {
+                               cur.posBackward();
+                               cur.pushBackward(*cur.nextInset());
+                       }
+               }
                if (c == '{')
                        cur.niceInsert(MathAtom(new InsetMathBrace));
                else if (c != ' ')
@@ -1554,6 +1568,19 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                return true;
        }
 
+       if (cur.pos() > 0) {
+               MathMacro const * macro = cur.inset().asInsetMath()->asMacro();
+               if (macro) {
+                       // resume macro_mode
+                       docstring const & s = macro->name();
+                       cur.leaveInset(*macro);
+                       cur.plainErase();
+                       docstring safe = grabAndEraseSelection(cur);
+                       cur.insert(MathAtom(new InsetMathUnknown("\\" + s + c, safe, false)));
+                       return true;    
+               }
+       }
+
        selClearOrDel(cur);
 
        if (c == '\n') {
@@ -1615,6 +1642,13 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                        cur.niceInsert(createInsetMath("sim"));
                        return true;
                }
+               if (!isAsciiOrMathAlpha(c)) {
+                       MathAtom at = createInsetMath("text");
+                       at.nucleus()->cell(0).push_back(MathAtom(new InsetMathChar(c)));
+                       cur.niceInsert(at);
+                       cur.posForward();
+                       return true;
+               }
        } else {
                if (c == '^') {
                        cur.niceInsert(createInsetMath("textasciicircum"));
@@ -1634,7 +1668,8 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
 
 
        // try auto-correction
-       if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0 && math_autocorrect(cur.prevAtom(), c))
+       if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
+                 && math_autocorrect(cur.prevAtom(), c))
                return true;
 
        // no special circumstances, so insert the character without any fuss
@@ -1927,6 +1962,9 @@ MathCompletionList::MathCompletionList(Cursor const & cur)
        globals.push_back(from_ascii("\\color"));
        globals.push_back(from_ascii("\\normalcolor"));
        globals.push_back(from_ascii("\\textcolor"));
+       globals.push_back(from_ascii("\\cfrac"));
+       globals.push_back(from_ascii("\\cfracleft"));
+       globals.push_back(from_ascii("\\cfracright"));
        globals.push_back(from_ascii("\\dfrac"));
        globals.push_back(from_ascii("\\tfrac"));
        globals.push_back(from_ascii("\\dbinom"));