]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathMacroTemplate.cpp
revert last patch. there's something wrong, possibly unrelated to this
[lyx.git] / src / mathed / MathMacroTemplate.cpp
index fd0a5874bbd4e021bb8194b55c406be5b6204f0a..3087919d2a80874dcb952d1e5d34fba06c822de3 100644 (file)
@@ -400,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), argsInLook_(0),
+       : InsetMathNest(optionals + 3), numargs_(numargs), argsInLook_(numargs),
          optionals_(optionals), optionalValues_(optionalValues),
          type_(type), lookOutdated_(true)
 {
@@ -465,7 +465,7 @@ void MathMacroTemplate::updateToContext(MacroContext const & mc) const
 }
 
 
-void MathMacroTemplate::updateLook(bool editing) const
+void MathMacroTemplate::updateLook() const
 {
        lookOutdated_ = true;
 }
@@ -486,14 +486,13 @@ void MathMacroTemplate::createLook(int args) const
        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) {
-                       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_) {
+                       // color it light grey, if it is to be removed when the cursor leaves
+                       if (i == argsInLook_) {
                                optData->push_back(MathAtom(
                                        new InsetColoredCell(Color_mathbg, Color_mathmacrooldarg)));
-                               optData = &(*optData)[0].nucleus()->cell(0);
+                               optData = &(*optData)[optData->size() - 1].nucleus()->cell(0);
                        }
 
                        optData->push_back(MathAtom(new InsetMathChar('[')));
@@ -598,9 +597,8 @@ void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
        pi.pain.rectangle(x, a, w, h, Color_mathframe);
 
        // just to be sure: set some dummy values for coord cache
-       for (idx_type i = 0; i < nargs(); ++i) {
+       for (idx_type i = 0; i < nargs(); ++i)
                cell(i).setXY(*pi.base.bv, x, y);
-       }
 
        // draw contents
        look_.draw(pi, x + 3, y);
@@ -609,15 +607,21 @@ void MathMacroTemplate::draw(PainterInfo & pi, int x, int y) const
 
 void MathMacroTemplate::edit(Cursor & cur, bool front, EntryDirection entry_from)
 {
-       updateLook(true);
-       cur.updateFlags(Update::Force);
+       updateLook();
+       cur.updateFlags(Update::SinglePar);
        InsetMathNest::edit(cur, front, entry_from);
 }
 
 
 bool MathMacroTemplate::notifyCursorLeaves(Cursor const & old, Cursor & cur)
 {
-       commitEditChanges(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(old, cur);
@@ -652,7 +656,7 @@ void MathMacroTemplate::shiftArguments(size_t from, int by) {
                if (it.nextInset()->lyxCode() != MATHMACROARG_CODE)
                        continue;
                MathMacroArgument * arg = static_cast<MathMacroArgument*>(it.nextInset());
-               if (arg->number() >= from + 1)
+               if (arg->number() >= int(from) + 1)
                        arg->setNumber(arg->number() + by);
        }
 
@@ -1127,26 +1131,58 @@ void MathMacroTemplate::write(WriteStream & os) const
 
 void MathMacroTemplate::write(WriteStream & os, bool overwriteRedefinition) const
 {
-       // newcommand or renewcommand
-       if (os.latex() && optionals_ > 1)
-               os << "\\newlyxcommand";
-       else {
+       if (os.latex()) {
+               if (optionals_ > 0) {
+                       // macros with optionals use the xargs package, e.g.:
+                       // \newcommandx{\foo}[2][usedefault, addprefix=\global,1=default]{#1,#2}
+                       if (redefinition_ && !overwriteRedefinition)
+                               os << "\\renewcommandx";
+                       else
+                               os << "\\newcommandx";
+
+                       os << "\\" << name().c_str()
+                          << "[" << numargs_ << "]"
+                          << "[usedefault, addprefix=\\global";
+                       for (int i = 0; i < optionals_; ++i) {
+                               docstring optValue = asString(cell(optIdx(i)));
+                               if (optValue.find(']') != docstring::npos
+                                   || optValue.find(',') != docstring::npos)
+                                       os << ", " << i + 1 << "="
+                                       << "{" << cell(optIdx(i)) << "}";
+                               else
+                                       os << ", " << i + 1 << "="
+                                       << cell(optIdx(i));
+                       }
+                       os << "]";
+               } else {
+                       // macros without optionals use standard _global_ \def macros:
+                       // \global\def\foo#1#2{#1,#2}
+                       os << "\\global\\def\\" << name().c_str();
+                       docstring param = from_ascii("#0");
+                       for (int i = 1; i <= numargs_; ++i) { 
+                               param[1] = '0' + i;
+                               os << param;
+                       }
+               }
+       } else {
+               // in LyX output we use some pseudo syntax which is implementation
+               // independent, e.g.
+               // \newcommand{\foo}[2][default}{#1,#2}
                if (redefinition_ && !overwriteRedefinition)
                        os << "\\renewcommand";
                else
                        os << "\\newcommand";
-       }
-       os << "{\\" << name().c_str() << '}';
-       if (numargs_ > 0)
-               os << '[' << numargs_ << ']';
-
-       // optional values
-       for (int i = 0; i < optionals_; ++i) {
-               docstring optValue = asString(cell(optIdx(i)));
-               if (optValue.find(']') != docstring::npos)
-                       os << "[{" << cell(optIdx(i)) << "}]";
-               else
-                       os << "[" << cell(optIdx(i)) << "]";
+               os << "{\\" << name().c_str() << '}';
+               if (numargs_ > 0)
+                       os << '[' << numargs_ << ']';
+
+               for (int i = 0; i < optionals_; ++i) {
+                       docstring optValue = asString(cell(optIdx(i)));
+                       if (optValue.find(']') != docstring::npos)
+                               os << "[{" << cell(optIdx(i)) << "}]";
+                       else
+                               os << "[" << cell(optIdx(i)) << "]";
+               }
        }
 
        os << "{" << cell(defIdx()) << "}";
@@ -1232,9 +1268,11 @@ bool MathMacroTemplate::fixNameAndCheckIfValid()
        
 void MathMacroTemplate::validate(LaTeXFeatures & features) const
 {
-       if (optionals_ > 1) {
-               features.require("newlyxcommand");
-       }
+       // we need global optional macro arguments. They are not available 
+       // with \def, and \newcommand does not support global macros. So we
+       // are bound to xargs also for the single-optional-parameter case.
+       if (optionals_ > 0)
+               features.require("xargs");
 }
 
 void MathMacroTemplate::getDefaults(vector<docstring> & defaults) const
@@ -1274,4 +1312,10 @@ void MathMacroTemplate::infoize(odocstream & os) const
        os << "Math Macro: \\" << name();
 }
 
+
+docstring MathMacroTemplate::contextMenu(BufferView const &, int, int) const
+{
+       return from_ascii("context-math-macro-definition");
+}
+
 } // namespace lyx