]> git.lyx.org Git - features.git/blobdiff - src/mathed/InsetMathNest.cpp
* Lazy MathData to avoid unneeded interpretation of macro definitions
[features.git] / src / mathed / InsetMathNest.cpp
index 2c2c8a67208eacf89a336c21a40bb2d6235c352a..00d4e2ac6e0c7759f4e48fd7cd99221dc907f65d 100644 (file)
@@ -20,7 +20,6 @@
 #include "InsetMathComment.h"
 #include "InsetMathDelim.h"
 #include "InsetMathHull.h"
-//#include "InsetMathMBox.h"
 #include "InsetMathRef.h"
 #include "InsetMathScript.h"
 #include "InsetMathSpace.h"
@@ -28,6 +27,7 @@
 #include "InsetMathUnknown.h"
 #include "MathData.h"
 #include "MathFactory.h"
+#include "MathMacro.h"
 #include "MathMacroArgument.h"
 #include "MathParser.h"
 #include "MathStream.h"
 
 #include "Bidi.h"
 #include "BufferView.h"
-#include "Color.h"
 #include "CoordCache.h"
 #include "Cursor.h"
 #include "CutAndPaste.h"
-#include "debug.h"
+#include "support/debug.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
 #include "LyXFunc.h"
-#include "gettext.h"
+#include "support/gettext.h"
 #include "Text.h"
 #include "OutputParams.h"
 
 #include "support/lstrings.h"
 #include "support/textutils.h"
+#include "support/docstream.h"
 
 #include "frontends/Clipboard.h"
 #include "frontends/Painter.h"
@@ -59,6 +59,8 @@
 
 #include <sstream>
 
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 
@@ -68,10 +70,6 @@ using cap::cutSelection;
 using cap::replaceSelection;
 using cap::selClearOrDel;
 
-using std::endl;
-using std::string;
-using std::istringstream;
-
 
 InsetMathNest::InsetMathNest(idx_type nargs)
        : cells_(nargs), lock_(false), mouse_hover_(false)
@@ -111,7 +109,7 @@ void InsetMathNest::cursorPos(BufferView const & bv,
 // to touch all (math)inset's draw() methods. Right now, we'll store
 // absolute value, and make them here relative, only to make them
 // absolute again when actually drawing the cursor. What a mess.
-       BOOST_ASSERT(ptr_cmp(&sl.inset(), this));
+       BOOST_ASSERT(&sl.inset() == this);
        MathData const & ar = sl.cell();
        CoordCache const & coord_cache = bv.coordCache();
        if (!coord_cache.getArrays().has(&ar)) {
@@ -132,7 +130,7 @@ void InsetMathNest::cursorPos(BufferView const & bv,
        }
        Point const pt2 = coord_cache.getInsets().xy(this);
        //lyxerr << "retrieving position cache for MathData "
-       //      << pt.x_ << ' ' << pt.y_ << std::endl;
+       //      << pt.x_ << ' ' << pt.y_ << endl;
        x = pt.x_ - pt2.x_ + ar.pos2x(sl.pos());
        y = pt.y_ - pt2.y_;
 //     lyxerr << "pt.y_ : " << pt.y_ << " pt2_.y_ : " << pt2.y_
@@ -156,7 +154,7 @@ void InsetMathNest::metrics(MetricsInfo const & mi) const
 
 bool InsetMathNest::idxNext(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (cur.idx() == cur.lastidx())
                return false;
        ++cur.idx();
@@ -165,7 +163,7 @@ bool InsetMathNest::idxNext(Cursor & cur) const
 }
 
 
-bool InsetMathNest::idxRight(Cursor & cur) const
+bool InsetMathNest::idxForward(Cursor & cur) const
 {
        return idxNext(cur);
 }
@@ -173,7 +171,7 @@ bool InsetMathNest::idxRight(Cursor & cur) const
 
 bool InsetMathNest::idxPrev(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (cur.idx() == 0)
                return false;
        --cur.idx();
@@ -182,7 +180,7 @@ bool InsetMathNest::idxPrev(Cursor & cur) const
 }
 
 
-bool InsetMathNest::idxLeft(Cursor & cur) const
+bool InsetMathNest::idxBackward(Cursor & cur) const
 {
        return idxPrev(cur);
 }
@@ -190,7 +188,7 @@ bool InsetMathNest::idxLeft(Cursor & cur) const
 
 bool InsetMathNest::idxFirst(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (nargs() == 0)
                return false;
        cur.idx() = 0;
@@ -201,7 +199,7 @@ bool InsetMathNest::idxFirst(Cursor & cur) const
 
 bool InsetMathNest::idxLast(Cursor & cur) const
 {
-       BOOST_ASSERT(ptr_cmp(&cur.inset(), this));
+       BOOST_ASSERT(&cur.inset() == this);
        if (nargs() == 0)
                return false;
        cur.idx() = cur.lastidx();
@@ -229,7 +227,7 @@ void InsetMathNest::draw(PainterInfo & pi, int x, int y) const
 #if 0
        if (lock_)
                pi.pain.fillRectangle(x, y - ascent(), width(), height(),
-                                       Color::mathlockbg);
+                                       Color_mathlockbg);
 #endif
        setPosCache(pi, x, y);
 }
@@ -242,13 +240,14 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
        Cursor & cur = bv.cursor();
        if (!cur.selection())
                return;
-       if (!ptr_cmp(&cur.inset(), this))
+       if (&cur.inset() != this)
                return;
 
        // FIXME: hack to get position cache warm
+       bool const original_drawing_state = pi.pain.isDrawingEnabled();
        pi.pain.setDrawingEnabled(false);
        draw(pi, x, y);
-       pi.pain.setDrawingEnabled(true);
+       pi.pain.setDrawingEnabled(original_drawing_state);
 
        CursorSlice s1 = cur.selBegin();
        CursorSlice s2 = cur.selEnd();
@@ -262,7 +261,7 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
                int y1 = g.pos.y_ - g.dim.ascent();
                int x2 = g.pos.x_ + c.pos2x(s2.pos());
                int y2 = g.pos.y_ + g.dim.descent();
-               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
+               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
        //lyxerr << "InsetMathNest::drawing selection 3: "
        //      << " x1: " << x1 << " x2: " << x2
        //      << " y1: " << y1 << " y2: " << y2 << endl;
@@ -275,7 +274,7 @@ void InsetMathNest::drawSelection(PainterInfo & pi, int x, int y) const
                                int y1 = g.pos.y_ - g.dim.ascent();
                                int x2 = g.pos.x_ + g.dim.width();
                                int y2 = g.pos.y_ + g.dim.descent();
-                               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color::selection);
+                               pi.pain.fillRectangle(x1, y1, x2 - x1, y2 - y1, Color_selection);
                        }
                }
        }
@@ -432,8 +431,8 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
        Font font;
        bool b;
        font.fromString(to_utf8(arg), b);
-       if (font.color() != Color::inherit) {
-               MathAtom at = MathAtom(new InsetMathColor(true, font.color()));
+       if (font.fontInfo().color() != Color_inherit) {
+               MathAtom at = MathAtom(new InsetMathColor(true, font.fontInfo().color()));
                cur.handleNest(at, 0);
        }
 }
@@ -441,7 +440,7 @@ void InsetMathNest::handleFont2(Cursor & cur, docstring const & arg)
 
 void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 {
-       //lyxerr << "InsetMathNest: request: " << cmd << std::endl;
+       //lyxerr << "InsetMathNest: request: " << cmd << endl;
        //CursorSlice sl = cur.current();
 
        switch (cmd.action) {
@@ -508,10 +507,10 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.clearTargetX();
                cur.macroModeClose();
                if (cur.pos() != cur.lastpos() && cur.openable(cur.nextAtom())) {
-                       cur.pushLeft(*cur.nextAtom().nucleus());
+                       cur.pushBackward(*cur.nextAtom().nucleus());
                        cur.inset().idxFirst(cur);
-               } else if (cur.posRight() || idxRight(cur)
-                       || cur.popRight() || cur.selection())
+               } else if (cur.posForward() || idxForward(cur)
+                       || cur.popForward() || cur.selection())
                        ;
                else {
                        cmd = FuncRequest(LFUN_FINISHED_FORWARD);
@@ -527,11 +526,11 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.clearTargetX();
                cur.macroModeClose();
                if (cur.pos() != 0 && cur.openable(cur.prevAtom())) {
-                       cur.posLeft();
+                       cur.posBackward();
                        cur.push(*cur.nextAtom().nucleus());
                        cur.inset().idxLast(cur);
-               } else if (cur.posLeft() || idxLeft(cur)
-                       || cur.popLeft() || cur.selection())
+               } else if (cur.posBackward() || idxBackward(cur)
+                       || cur.popBackward() || cur.selection())
                        ;
                else {
                        cmd = FuncRequest(LFUN_FINISHED_BACKWARD);
@@ -543,26 +542,24 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
        case LFUN_CHAR_RIGHT_SELECT:
                //FIXME: for visual cursor, really move right
                if (reverseDirectionNeeded(cur))
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
-                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
+                       cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
+                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
                else 
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
-                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
+                       cmd.action = cmd.action == LFUN_CHAR_RIGHT_SELECT ? 
+                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
+               doDispatch(cur, cmd);
                break;
 
        case LFUN_CHAR_LEFT:
        case LFUN_CHAR_LEFT_SELECT:
                //FIXME: for visual cursor, really move left
                if (reverseDirectionNeeded(cur))
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_LEFT_SELECT ? 
-                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD));
+                       cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ? 
+                                       LFUN_CHAR_FORWARD_SELECT : LFUN_CHAR_FORWARD;
                else 
-                       lyx::dispatch(FuncRequest(
-                               cmd.action == LFUN_CHAR_LEFT_SELECT ? 
-                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD));
+                       cmd.action = cmd.action == LFUN_CHAR_LEFT_SELECT ? 
+                                       LFUN_CHAR_BACKWARD_SELECT : LFUN_CHAR_BACKWARD;
+               doDispatch(cur, cmd);
                break;
 
        case LFUN_DOWN:
@@ -621,10 +618,13 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_LINE_BEGIN:
        case LFUN_WORD_BACKWARD:
+       case LFUN_WORD_LEFT:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_LINE_BEGIN_SELECT:
        case LFUN_WORD_BACKWARD_SELECT:
+       case LFUN_WORD_LEFT_SELECT:
                cur.selHandle(cmd.action == LFUN_WORD_BACKWARD_SELECT ||
+                               cmd.action == LFUN_WORD_LEFT_SELECT || 
                                cmd.action == LFUN_LINE_BEGIN_SELECT);
                cur.macroModeClose();
                if (cur.pos() != 0) {
@@ -642,11 +642,14 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                break;
 
        case LFUN_WORD_FORWARD:
+       case LFUN_WORD_RIGHT:
        case LFUN_LINE_END:
                cur.updateFlags(Update::Decoration | Update::FitCursor);
        case LFUN_WORD_FORWARD_SELECT:
+       case LFUN_WORD_RIGHT_SELECT:
        case LFUN_LINE_END_SELECT:
                cur.selHandle(cmd.action == LFUN_WORD_FORWARD_SELECT ||
+                               cmd.action == LFUN_WORD_RIGHT_SELECT ||
                                cmd.action == LFUN_LINE_END_SELECT);
                cur.macroModeClose();
                cur.clearTargetX();
@@ -721,10 +724,12 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                }
                break;
 
+       // 'Locks' the math inset. A 'locked' math inset behaves as a unit
+       // that is traversed by a single <CursorLeft>/<CursorRight>.
        case LFUN_INSET_TOGGLE:
                cur.recordUndo();
                lock(!lock());
-               cur.popRight();
+               cur.popForward();
                break;
 
        case LFUN_SELF_INSERT:
@@ -755,8 +760,8 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                    && cur.macroModeClose()) {
                        MathAtom const atom = cur.prevAtom();
                        if (atom->asNestInset() && atom->isActive()) {
-                               cur.posLeft();
-                               cur.pushLeft(*cur.nextInset());
+                               cur.posBackward();
+                               cur.pushBackward(*cur.nextInset());
                        }
                } else if (!interpretChar(cur, cmd.argument()[0])) {
                        cmd = FuncRequest(LFUN_FINISHED_FORWARD);
@@ -876,8 +881,8 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                selClearOrDel(cur);
                //cur.plainInsert(MathAtom(new InsetMathMBox(cur.bv())));
                cur.plainInsert(MathAtom(new InsetMathBox(from_ascii("mbox"))));
-               cur.posLeft();
-               cur.pushLeft(*cur.nextInset());
+               cur.posBackward();
+               cur.pushBackward(*cur.nextInset());
                cur.niceInsert(save_selection);
 #else
                if (currentMode() == Inset::TEXT_MODE) {
@@ -918,7 +923,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_DELIM: {
                docstring ls;
-               docstring rs = support::split(cmd.argument(), ls, ' ');
+               docstring rs = split(cmd.argument(), ls, ' ');
                // Reasonable default values
                if (ls.empty())
                        ls = '(';
@@ -982,6 +987,22 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                cur.recordUndo();
                interpretChar(cur, '^');
                break;
+               
+       case LFUN_MATH_MACRO_FOLD:
+       case LFUN_MATH_MACRO_UNFOLD: {
+               Cursor it = cur;
+               bool fold = cmd.action == LFUN_MATH_MACRO_FOLD;
+               bool found = findMacroToFoldUnfold(it, fold);
+               if (found) {
+                       MathMacro * macro = it.nextInset()->asInsetMath()->asMacro();
+                       cur.recordUndoInset();
+                       if (fold)
+                               macro->fold(cur);
+                       else
+                               macro->unfold(cur);
+               }
+               break;
+       }
 
        case LFUN_QUOTE_INSERT:
                // interpret this as if a straight " was typed
@@ -1008,7 +1029,7 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
                        InsetMathRef tmp(name);
                        data = tmp.createDialogStr(to_utf8(name));
                }
-               cur.bv().showInsetDialog(to_utf8(name), data, 0);
+               cur.bv().showDialog(to_utf8(name), data);
                break;
        }
 
@@ -1035,6 +1056,40 @@ void InsetMathNest::doDispatch(Cursor & cur, FuncRequest & cmd)
 }
 
 
+bool InsetMathNest::findMacroToFoldUnfold(Cursor & it, bool fold) const {
+       // look for macro to open/close, but stay in mathed
+       for (; !it.empty(); it.pop_back()) {
+                       
+               // go backward through the current cell
+               Inset * inset = it.nextInset();
+               while (inset && inset->asInsetMath()) {
+                       MathMacro * macro = inset->asInsetMath()->asMacro();
+                       if (macro) {
+                               // found the an macro to open/close?
+                               if (macro->folded() != fold)
+                                       return true;
+                               
+                               // Wrong folding state.
+                               // If this was the first we see in this slice, look further left,
+                               // otherwise go up.
+                               if (inset != it.nextInset())
+                                       break;
+                       }
+                       
+                       // go up if this was the left most position
+                       if (it.pos() == 0)
+                               break;
+                       
+                       // go left
+                       it.pos()--;
+                       inset = it.nextInset();
+               }
+       }
+       
+       return false;
+}
+
+
 bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                FuncStatus & flag) const
 {
@@ -1126,12 +1181,16 @@ bool InsetMathNest::getStatus(Cursor & cur, FuncRequest const & cmd,
                // Don't do this with multi-cell selections
                flag.enabled(cur.selBegin().idx() == cur.selEnd().idx());
                break;
-
-       case LFUN_HYPHENATION_POINT_INSERT:
-       case LFUN_LIGATURE_BREAK_INSERT:
-       case LFUN_MENU_SEPARATOR_INSERT:
-       case LFUN_DOTS_INSERT:
-       case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
+               
+       case LFUN_MATH_MACRO_FOLD:
+       case LFUN_MATH_MACRO_UNFOLD: {
+               Cursor it = cur;
+               bool found = findMacroToFoldUnfold(it, cmd.action == LFUN_MATH_MACRO_FOLD);
+               flag.enabled(found);
+               break;
+       }
+               
+       case LFUN_SPECIALCHAR_INSERT:
                // FIXME: These would probably make sense in math-text mode
                flag.enabled(false);
                break;
@@ -1401,7 +1460,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type c)
                        return true;
                }
 
-               if (cur.popRight()) {
+               if (cur.popForward()) {
                        // FIXME: we have to enable full redraw here because of the
                        // visual box corners that define the inset. If we know for
                        // sure that we stay within the same cell we can optimize for