]> git.lyx.org Git - lyx.git/blobdiff - src/Cursor.cpp
Avoid duplicating mode changing commands
[lyx.git] / src / Cursor.cpp
index b4ca8e0c656a69deacc794185c46e56a50331a22..4e63734155a302e6a4b7abc2e8de48de6e0eaf99 100644 (file)
@@ -31,6 +31,7 @@
 #include "Paragraph.h"
 #include "ParIterator.h"
 #include "Row.h"
+#include "texstream.h"
 #include "Text.h"
 #include "TextMetrics.h"
 #include "TocBackend.h"
 
 #include "mathed/InsetMath.h"
 #include "mathed/InsetMathBrace.h"
+#include "mathed/InsetMathEnsureMath.h"
 #include "mathed/InsetMathScript.h"
 #include "mathed/MacroTable.h"
 #include "mathed/MathData.h"
+#include "mathed/MathFactory.h"
 #include "mathed/MathMacro.h"
 
-#include "support/bind.h"
-
 #include <sstream>
 #include <limits>
 #include <map>
@@ -997,18 +998,18 @@ DocIterator Cursor::selectionEnd() const
 
 void Cursor::setSelection()
 {
-       setSelection(true);
+       selection(true);
        if (idx() == normalAnchor().idx() &&
            pit() == normalAnchor().pit() &&
            pos() == normalAnchor().pos())
-               setSelection(false);
+               selection(false);
 }
 
 
 void Cursor::setSelection(DocIterator const & where, int n)
 {
        setCursor(where);
-       setSelection(true);
+       selection(true);
        anchor_ = where;
        pos() += n;
 }
@@ -1016,7 +1017,7 @@ void Cursor::setSelection(DocIterator const & where, int n)
 
 void Cursor::clearSelection()
 {
-       setSelection(false);
+       selection(false);
        setWordSelection(false);
        setMark(false);
        resetAnchor();
@@ -1079,7 +1080,7 @@ bool Cursor::selHandle(bool sel)
                cap::saveSelection(*this);
 
        resetAnchor();
-       setSelection(sel);
+       selection(sel);
        return true;
 }
 } // namespace lyx
@@ -1184,9 +1185,8 @@ void Cursor::plainInsert(MathAtom const & t)
 
 void Cursor::insert(docstring const & str)
 {
-       for_each(str.begin(), str.end(),
-                bind(static_cast<void(Cursor::*)(char_type)>
-                            (&Cursor::insert), this, _1));
+       for (char_type c : str)
+               insert(c);
 }
 
 
@@ -1319,7 +1319,7 @@ bool Cursor::backspace()
                // let's require two backspaces for 'big stuff' and
                // highlight on the first
                resetAnchor();
-               setSelection(true);
+               selection(true);
                --pos();
        } else {
                --pos();
@@ -1366,7 +1366,7 @@ bool Cursor::erase()
        // 'clever' UI hack: only erase large items if previously slected
        if (pos() != lastpos() && nextAtom()->nargs() > 0) {
                resetAnchor();
-               setSelection(true);
+               selection(true);
                ++pos();
        } else {
                plainErase();
@@ -1449,7 +1449,22 @@ bool Cursor::macroModeClose()
        else if (atom.nucleus()->nargs() > 0)
                atom.nucleus()->cell(0).append(selection);
 
-       plainInsert(atom);
+       if (in->currentMode() == Inset::TEXT_MODE
+           && atom.nucleus()->currentMode() == Inset::MATH_MODE
+           && name != from_ascii("ensuremath")) {
+               MathAtom at(new InsetMathEnsureMath(buffer()));
+               at.nucleus()->cell(0).push_back(atom);
+               niceInsert(at);
+               posForward();
+       } else if (in->currentMode() == Inset::MATH_MODE
+                  && atom.nucleus()->currentMode() == Inset::TEXT_MODE
+                  && name != from_ascii("text")) {
+               MathAtom at = createInsetMath("text", buffer());
+               at.nucleus()->cell(0).push_back(atom);
+               niceInsert(at);
+               posForward();
+       } else
+               plainInsert(atom);
 
        // finally put the macro argument behind, if needed
        if (macroArg) {
@@ -1569,8 +1584,7 @@ void Cursor::normalize()
                        << pos() << ' ' << lastpos() <<  " in idx: " << idx()
                       << " in atom: '";
                odocstringstream os;
-               TexRow texrow(false);
-               otexrowstream ots(os,texrow);
+               otexrowstream ots(os, false);
                WriteStream wi(ots, false, true, WriteStream::wsDefault);
                inset().asInsetMath()->write(wi);
                lyxerr << to_utf8(os.str()) << endl;