]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacroTemplate.cpp
Change the interface to a paragraph's layout. We still store a LayoutPtr, but now...
[lyx.git] / src / mathed / MathMacroTemplate.cpp
index 68926d47aef40849a248fe4a91224d20c6cc7589..ff296e80b56f387877044924b862d27bd3d6d9a5 100644 (file)
 #include "BufferView.h"
 #include "Color.h"
 #include "Cursor.h"
-#include "support/debug.h"
 #include "DispatchResult.h"
 #include "FuncRequest.h"
 #include "FuncStatus.h"
-#include "support/gettext.h"
 #include "Lexer.h"
 #include "Undo.h"
 
-#include "frontends/FontMetrics.h"
 #include "frontends/Painter.h"
 
 #include "support/convert.h"
+#include "support/debug.h"
+#include "support/gettext.h"
 #include "support/docstream.h"
 #include "support/lstrings.h"
 
-#include "support/debug.h"
-
 #include <sstream>
 
 using namespace std;
@@ -80,8 +77,8 @@ protected:
 
 
 InsetLabelBox::InsetLabelBox(MathAtom const & atom, docstring label,
-                            MathMacroTemplate const & parent, bool frame)
-:      InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
+       MathMacroTemplate const & parent, bool frame)
+       : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
 {
        cell(0).insert(0, atom);
 }
@@ -89,7 +86,7 @@ InsetLabelBox::InsetLabelBox(MathAtom const & atom, docstring label,
 
 InsetLabelBox::InsetLabelBox(docstring label,
                             MathMacroTemplate const & parent, bool frame)
-:      InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
+       : InsetMathNest(1), parent_(parent), label_(label), frame_(frame)
 {
 }
 
@@ -137,8 +134,6 @@ void InsetLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
                if (!parent_.premetrics())
                        dim.des += maxasc + maxdes + 1;
        }
-
-       setDimCache(mi, dim);
 }
 
 
@@ -221,7 +216,6 @@ void DisplayLabelBox::metrics(MetricsInfo & mi, Dimension & dim) const
                dim.wid = 0;
                dim.asc = 0;
                dim.des = 0;
-               setDimCache(mi, dim);
        }
 }
 
@@ -279,6 +273,61 @@ void InsetMathWrapper::draw(PainterInfo & pi, int x, int y) const
 }
 
 
+///////////////////////////////////////////////////////////////////////
+class InsetColoredCell : public InsetMathNest {
+public:
+       ///
+       InsetColoredCell(ColorCode min, ColorCode max);
+       ///
+       InsetColoredCell(ColorCode min, ColorCode max, MathAtom const & atom);
+       ///
+       void draw(PainterInfo &, int x, int y) const;
+       ///
+       void metrics(MetricsInfo & mi, Dimension & dim) const;
+
+protected:
+       ///
+       Inset * clone() const;
+       ///
+       ColorCode min_;
+       ///
+       ColorCode max_;
+};
+
+
+InsetColoredCell::InsetColoredCell(ColorCode min, ColorCode max)
+       : InsetMathNest(1), min_(min), max_(max)
+{
+}
+
+
+InsetColoredCell::InsetColoredCell(ColorCode min, ColorCode max, MathAtom const & atom)
+       : InsetMathNest(1), min_(min), max_(max)
+{
+       cell(0).insert(0, atom);
+}
+
+
+Inset * InsetColoredCell::clone() const
+{
+       return new InsetColoredCell(*this);
+}
+
+
+void InsetColoredCell::metrics(MetricsInfo & mi, Dimension & dim) const
+{
+       cell(0).metrics(mi, dim);
+}
+
+
+void InsetColoredCell::draw(PainterInfo & pi, int x, int y) const
+{
+       pi.pain.enterMonochromeMode(min_, max_);
+       cell(0).draw(pi, x, y);
+       pi.pain.leaveMonochromeMode();
+}
+
+
 ///////////////////////////////////////////////////////////////////////
 
 class InsetNameWrapper : public InsetMathWrapper {
@@ -340,7 +389,7 @@ void InsetNameWrapper::draw(PainterInfo & pi, int x, int y) const
 
 
 MathMacroTemplate::MathMacroTemplate()
-       : InsetMathNest(3), numargs_(0), optionals_(0),
+       : InsetMathNest(3), numargs_(0), argsInLook_(0), optionals_(0),
          type_(MacroTypeNewcommand), lookOutdated_(true)
 {
        initMath();
@@ -351,7 +400,7 @@ MathMacroTemplate::MathMacroTemplate(docstring const & name, int numargs,
        int optionals, MacroType type,
        vector<MathData> const & optionalValues,
        MathData const & def, MathData const & display)
-       : InsetMathNest(optionals + 3), numargs_(numargs),
+       : InsetMathNest(optionals + 3), numargs_(numargs), argsInLook_(0),
          optionals_(optionals), optionalValues_(optionalValues),
          type_(type), lookOutdated_(true)
 {
@@ -396,7 +445,11 @@ MathMacroTemplate::MathMacroTemplate(docstring const & str)
 
 Inset * MathMacroTemplate::clone() const
 {
-       return new MathMacroTemplate(*this);
+       MathMacroTemplate * inset = new MathMacroTemplate(*this);
+       // the parent pointers of the proxy insets above will point to
+       // to the old template. Hence, the look must be updated.
+       inset->updateLook();
+       return inset;
 }
 
 
@@ -418,9 +471,10 @@ void MathMacroTemplate::updateLook() const
 }
 
 
-void MathMacroTemplate::createLook() const
+void MathMacroTemplate::createLook(int args) const
 {
        look_.clear();
+       argsInLook_ = args;
 
        // \foo
        look_.push_back(MathAtom(new InsetLabelBox(_("Name"), *this, false)));
@@ -431,12 +485,20 @@ void MathMacroTemplate::createLook() const
        int i = 0;
        if (optionals_ > 0) {
                look_.push_back(MathAtom(new InsetLabelBox(_("optional"), *this, false)));
-               MathData & optData = look_[look_.size() - 1].nucleus()->cell(0);
-
+               
                for (; i < optionals_; ++i) {
-                       optData.push_back(MathAtom(new InsetMathChar('[')));
-                       optData.push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
-                       optData.push_back(MathAtom(new InsetMathChar(']')));
+                       MathData * optData = &look_[look_.size() - 1].nucleus()->cell(0);
+
+                       // color it red, if it is to be remove when the cursor leaves
+                       if (optionals_ > argsInLook_) {
+                               optData->push_back(MathAtom(
+                                       new InsetColoredCell(Color_mathbg, Color_mathmacrooldarg)));
+                               optData = &(*optData)[0].nucleus()->cell(0);
+                       }
+
+                       optData->push_back(MathAtom(new InsetMathChar('[')));
+                       optData->push_back(MathAtom(new InsetMathWrapper(&cell(1 + i))));
+                       optData->push_back(MathAtom(new InsetMathChar(']')));
                }
        }
 
@@ -444,9 +506,21 @@ void MathMacroTemplate::createLook() const
        for (; i < numargs_; ++i) {
                MathData arg;
                arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
-               look_.push_back(MathAtom(new InsetMathBrace(arg)));
+               if (i >= argsInLook_) {
+                       look_.push_back(MathAtom(new InsetColoredCell(
+                               Color_mathbg, Color_mathmacrooldarg,
+                               MathAtom(new InsetMathBrace(arg)))));
+               } else
+                       look_.push_back(MathAtom(new InsetMathBrace(arg)));
        }
-
+       for (; i < argsInLook_; ++i) {
+               MathData arg;
+               arg.push_back(MathAtom(new MathMacroArgument(i + 1)));
+               look_.push_back(MathAtom(new InsetColoredCell(
+                       Color_mathbg, Color_mathmacronewarg,
+                       MathAtom(new InsetMathBrace(arg)))));
+       }
+       
        // :=
        look_.push_back(MathAtom(new InsetMathChar(':')));
        look_.push_back(MathAtom(new InsetMathChar('=')));
@@ -478,9 +552,10 @@ void MathMacroTemplate::metrics(MetricsInfo & mi, Dimension & dim) const
        }
 
        // update look?
-       if (lookOutdated_) {
+       int argsInDef = maxArgumentInDefinition();
+       if (lookOutdated_ || argsInDef != argsInLook_) {
                lookOutdated_ = false;
-               createLook();
+               createLook(argsInDef);
        }
 
        /// metrics for inset contents
@@ -532,19 +607,26 @@ void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
 }
 
 
-void MathMacroTemplate::edit(Cursor & cur, bool left)
+void MathMacroTemplate::edit(Cursor & cur, bool front, EntryDirection entry_from)
 {
        updateLook();
-       cur.updateFlags(Update::Force);
-       InsetMathNest::edit(cur, left);
+       cur.updateFlags(Update::SinglePar);
+       InsetMathNest::edit(cur, front, entry_from);
 }
 
 
-bool MathMacroTemplate::notifyCursorLeaves(Cursor & cur)
+bool MathMacroTemplate::notifyCursorLeaves(Cursor const & old, Cursor & cur)
 {
+       // find this in cursor old
+       Cursor insetCur = old;
+       int scriptSlice = insetCur.find(this);
+       BOOST_ASSERT(scriptSlice != -1);
+       insetCur.cutOff(scriptSlice);
+       
+       commitEditChanges(insetCur);
        updateLook();
        cur.updateFlags(Update::Force);
-       return InsetMathNest::notifyCursorLeaves(cur);
+       return InsetMathNest::notifyCursorLeaves(old, cur);
 }
 
 
@@ -584,6 +666,74 @@ void MathMacroTemplate::shiftArguments(size_t from, int by) {
 }
 
 
+int MathMacroTemplate::maxArgumentInDefinition() const
+{
+       int maxArg = 0;
+       MathMacroTemplate * nonConst = const_cast<MathMacroTemplate *>(this);
+       DocIterator it = doc_iterator_begin(*nonConst);
+       it.idx() = defIdx();
+       for (; it; it.forwardChar()) {
+               if (!it.nextInset())
+                       continue;
+               if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
+                       continue;
+               MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
+               maxArg = std::max(int(arg->number()), maxArg);
+       }
+       return maxArg;
+}
+
+
+void MathMacroTemplate::insertMissingArguments(int maxArg)
+{
+       bool found[9] = { false, false, false, false, false, false, false, false, false };
+       idx_type idx = cell(displayIdx()).empty() ? defIdx() : displayIdx();
+
+       // search for #n macros arguments
+       DocIterator it = doc_iterator_begin(*this);
+       it.idx() = idx;
+       for (; it && it[0].idx() == idx; it.forwardChar()) {
+               if (!it.nextInset())
+                       continue;
+               if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
+                       continue;
+               MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
+               found[arg->number() - 1] = true;
+       }
+
+       // add missing ones
+       for (int i = 0; i < maxArg; ++i) {
+               if (found[i])
+                       continue;
+
+               cell(idx).push_back(MathAtom(new MathMacroArgument(i + 1)));
+       }
+}
+
+
+void MathMacroTemplate::changeArity(Cursor & cur, int newNumArg)
+{
+       // remove parameter which do not appear anymore in the definition
+       for (int i = numargs_; i > newNumArg; --i)
+               removeParameter(cur, numargs_ - 1, false);
+       
+       // add missing parameter
+       for (int i = numargs_; i < newNumArg; ++i)
+               insertParameter(cur, numargs_, false, false);
+}
+
+
+void MathMacroTemplate::commitEditChanges(Cursor & cur)
+{
+       int argsInDef = maxArgumentInDefinition();
+       if (argsInDef != numargs_) {
+               cur.recordUndoFullDocument();
+               changeArity(cur, argsInDef);
+       }
+       insertMissingArguments(argsInDef);
+}
+
+
 // FIXME: factorize those functions here with a functional style, maybe using Boost's function
 // objects?
 
@@ -675,16 +825,19 @@ void fixMacroInstancesFunctional(Cursor const & from,
 }
 
 
-void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy)
+void MathMacroTemplate::insertParameter(Cursor & cur, int pos, bool greedy, bool addarg)
 {
        if (pos <= numargs_ && pos >= optionals_ && numargs_ < 9) {
                ++numargs_;
-               shiftArguments(pos, 1);
-
+               
                // append example #n
-               cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
-               if (!cell(displayIdx()).empty())
-                       cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
+               if (addarg) {
+                       shiftArguments(pos, 1);
+
+                       cell(defIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
+                       if (!cell(displayIdx()).empty())
+                               cell(displayIdx()).push_back(MathAtom(new MathMacroArgument(pos + 1)));
+               }
 
                if (!greedy) {
                        Cursor dit = cur;
@@ -767,7 +920,7 @@ void MathMacroTemplate::makeNonOptional(Cursor & cur) {
        if (numargs_ > 0 && optionals_ > 0) {
                --optionals_;
 
-               // store default value for later if the use changes his mind
+               // store default value for later if the user changes his mind
                optionalValues_[optionals_] = cell(optIdx(optionals_));
                cells_.erase(cells_.begin() + optIdx(optionals_));
 
@@ -802,6 +955,7 @@ void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_MACRO_ADD_PARAM:
                if (numargs_ < 9) {
+                       commitEditChanges(cur);
                        cur.recordUndoFullDocument();
                        size_t pos = numargs_;
                        if (arg.size() != 0)
@@ -813,6 +967,7 @@ void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_MACRO_REMOVE_PARAM:
                if (numargs_ > 0) {
+                       commitEditChanges(cur);
                        cur.recordUndoFullDocument();
                        size_t pos = numargs_ - 1;
                        if (arg.size() != 0)
@@ -823,6 +978,7 @@ void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_MACRO_APPEND_GREEDY_PARAM:
                if (numargs_ < 9) {
+                       commitEditChanges(cur);
                        cur.recordUndoFullDocument();
                        insertParameter(cur, numargs_, true);
                }
@@ -830,23 +986,27 @@ void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_MACRO_REMOVE_GREEDY_PARAM:
                if (numargs_ > 0) {
+                       commitEditChanges(cur);
                        cur.recordUndoFullDocument();
                        removeParameter(cur, numargs_ - 1, true);
                }
                break;
 
        case LFUN_MATH_MACRO_MAKE_OPTIONAL:
+               commitEditChanges(cur);
                cur.recordUndoFullDocument();
                makeOptional(cur);
                break;
 
        case LFUN_MATH_MACRO_MAKE_NONOPTIONAL:
+               commitEditChanges(cur);
                cur.recordUndoFullDocument();
                makeNonOptional(cur);
                break;
 
        case LFUN_MATH_MACRO_ADD_OPTIONAL_PARAM:
                if (numargs_ < 9) {
+                       commitEditChanges(cur);
                        cur.recordUndoFullDocument();
                        insertParameter(cur, optionals_);
                        makeOptional(cur);
@@ -855,12 +1015,14 @@ void MathMacroTemplate::doDispatch(Cursor & cur, FuncRequest & cmd)
 
        case LFUN_MATH_MACRO_REMOVE_OPTIONAL_PARAM:
                if (optionals_ > 0) {
+                       commitEditChanges(cur);
                        cur.recordUndoFullDocument();
                        removeParameter(cur, optionals_ - 1);
                } break;
 
        case LFUN_MATH_MACRO_ADD_GREEDY_OPTIONAL_PARAM:
                if (numargs_ == optionals_) {
+                       commitEditChanges(cur);
                        cur.recordUndoFullDocument();
                        insertParameter(cur, 0, true);
                        makeOptional(cur);
@@ -938,7 +1100,7 @@ bool MathMacroTemplate::getStatus(Cursor & /*cur*/, FuncRequest const & cmd,
 }
 
 
-void MathMacroTemplate::read(Buffer const &, Lexer & lex)
+void MathMacroTemplate::read(Lexer & lex)
 {
        MathData ar;
        mathed_parse_cell(ar, lex.getStream());
@@ -953,7 +1115,7 @@ void MathMacroTemplate::read(Buffer const &, Lexer & lex)
 }
 
 
-void MathMacroTemplate::write(Buffer const &, ostream & os) const
+void MathMacroTemplate::write(ostream & os) const
 {
        odocstringstream oss;
        WriteStream wi(oss, false, false);
@@ -975,7 +1137,7 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
        if (os.latex() && optionals_ > 1)
                os << "\\newlyxcommand";
        else {
-               if (redefinition_)
+               if (redefinition_ && !overwriteRedefinition)
                        os << "\\renewcommand";
                else
                        os << "\\newcommand";
@@ -1006,10 +1168,10 @@ void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) cons
 }
 
 
-int MathMacroTemplate::plaintext(Buffer const & buf, odocstream & os,
+int MathMacroTemplate::plaintext(odocstream & os,
                                 OutputParams const &) const
 {
-       static docstring const str = '[' + buf.B_("math macro") + ']';
+       static docstring const str = '[' + buffer().B_("math macro") + ']';
 
        os << str;
        return str.size();
@@ -1032,8 +1194,9 @@ bool MathMacroTemplate::validName() const
 
        // valid characters?
        for (size_t i = 0; i < n.size(); ++i) {
-               if (!(n[i] >= 'a' && n[i] <= 'z') &&
-                               !(n[i] >= 'A' && n[i] <= 'Z'))
+               if (!(n[i] >= 'a' && n[i] <= 'z')
+                   && !(n[i] >= 'A' && n[i] <= 'Z')
+                   && n[i] != '*')
                        return false;
        }