]> 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 efd473ba04bdc8850a92a4daa3928e232674fa29..c6e25b59945d0696f10b15e040631a79c724ae41 100644 (file)
 
 #include <config.h>
 
-//#define AUTOCORRECT
-
 #include "InsetMathNest.h"
 
 #include "InsetMathArray.h"
 #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"
 #include "InsetMathSpace.h"
 #include "InsetMathSymbol.h"
 #include "InsetMathUnknown.h"
-#ifdef AUTOCORRECT
 #include "MathAutoCorrect.h"
-#endif
 #include "MathCompletionList.h"
 #include "MathData.h"
 #include "MathFactory.h"
@@ -46,6 +44,7 @@
 #include "Cursor.h"
 #include "CutAndPaste.h"
 #include "DispatchResult.h"
+#include "Encoding.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LyXFunc.h"
@@ -518,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) {
 
@@ -818,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
@@ -950,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);
@@ -984,6 +992,12 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
        }
 
+       case LFUN_MATH_FONT_STYLE: {
+               FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, '\\' + cmd.argument());
+               doDispatch(cur, fr);
+               break;
+       }
+
        case LFUN_MATH_SIZE: {
                FuncRequest fr = FuncRequest(LFUN_MATH_INSERT, cmd.argument());
                doDispatch(cur, fr);
@@ -1240,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:
@@ -1257,18 +1267,22 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                flag.setEnabled(currentMode() != TEXT_MODE);
                break;
 
-       case LFUN_MATH_INSERT: {
+       case LFUN_MATH_FONT_STYLE: {
                bool const textarg =
-                       arg == "\\textbf"   || arg == "\\textsf" ||
-                       arg == "\\textrm"   || arg == "\\textmd" ||
-                       arg == "\\textit"   || arg == "\\textsc" ||
-                       arg == "\\textsl"   || arg == "\\textup" ||
-                       arg == "\\texttt"   || arg == "\\textbb" ||
-                       arg == "\\textnormal";
+                       arg == "textbf"   || arg == "textsf" ||
+                       arg == "textrm"   || arg == "textmd" ||
+                       arg == "textit"   || arg == "textsc" ||
+                       arg == "textsl"   || arg == "textup" ||
+                       arg == "texttt"   || arg == "textbb" ||
+                       arg == "textnormal";
                flag.setEnabled(currentMode() != TEXT_MODE || textarg);
                break;
        }
 
+       case LFUN_MATH_INSERT:
+               flag.setEnabled(currentMode() != TEXT_MODE);
+               break;
+
        case LFUN_MATH_MATRIX:
                flag.setEnabled(currentMode() == MATH_MODE);
                break;
@@ -1511,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 != ' ')
@@ -1519,22 +1539,18 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
                return true;
        }
 
-       // This is annoying as one has to press <space> far too often.
-       // Disable it.
 
-#ifdef AUTOCORRECT
-               // leave autocorrect mode if necessary
-               if (c == ' ' && cur.autocorrect()) {
-                       cur.autocorrect() = false;
-                       cur.message(_("Autocorrect Off ('!' to enter)"));
-                       return true;
-               } 
-               if (c == '!' && !cur.autocorrect()) {
-                       cur.autocorrect() = true;
-                       cur.message(_("Autocorrect On (<space> to exit)"));
-                       return true;
-               }
-#endif
+       // leave autocorrect mode if necessary
+       if (lyxrc.autocorrection_math && c == ' ' && cur.autocorrect()) {
+               cur.autocorrect() = false;
+               cur.message(_("Autocorrect Off ('!' to enter)"));
+               return true;
+       } 
+       if (lyxrc.autocorrection_math && c == '!' && !cur.autocorrect()) {
+               cur.autocorrect() = true;
+               cur.message(_("Autocorrect On (<space> to exit)"));
+               return true;
+       }
 
        // just clear selection on pressing the space bar
        if (cur.selection() && c == ' ') {
@@ -1552,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') {
@@ -1613,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"));
@@ -1631,20 +1667,19 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type const c)
        }
 
 
-#ifdef AUTOCORRECT
        // try auto-correction
-       if (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;
-#endif
 
        // no special circumstances, so insert the character without any fuss
        cur.insert(c);
-#ifdef AUTOCORRECT
-       if (!cur.autocorrect())
-               cur.message(_("Autocorrect Off ('!' to enter)"));
-       else
-               cur.message(_("Autocorrect On (<space> to exit)"));
-#endif
+       if (lyxrc.autocorrection_math) {
+               if (!cur.autocorrect())
+                       cur.message(_("Autocorrect Off ('!' to enter)"));
+               else
+                       cur.message(_("Autocorrect On (<space> to exit)"));
+       }
        return true;
 }
 
@@ -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"));