+ thisPos = (*cur)[thisSlice].pos();
+
+ // find arguments behind the macro
+ if (!interactiveInit) {
+ collectOptionalParameters(cur, macroOptionals, detachedArgs, p,
+ scriptToPutAround, macroPos, thisPos, thisSlice);
+ }
+ collectParameters(cur, macroNumArgs, detachedArgs, p,
+ scriptToPutAround, macroPos, thisPos, thisSlice, appetite);
+
+ // attach arguments back to macro inset
+ macroInset->attachArguments(detachedArgs, macroNumArgs, macroOptionals);
+
+ // found tail script? E.g. \foo{a}b^x
+ if (scriptToPutAround.nucleus()) {
+ InsetMathScript * scriptInset =
+ scriptToPutAround.nucleus()->asScriptInset();
+ // In the math parser we remove empty braces in the base
+ // of a script inset, but we have to restore them here.
+ if (scriptInset->nuc().empty()) {
+ MathData ar;
+ scriptInset->nuc().push_back(
+ MathAtom(new InsetMathBrace(ar)));
+ }
+ // put macro into a script inset
+ scriptInset->nuc()[0] = operator[](macroPos);
+ operator[](macroPos) = scriptToPutAround;
+
+ // go into the script inset nucleus
+ if (cur && thisPos == int(macroPos))
+ cur->append(0, 0);
+
+ // get pointer to "deep" copied macro inset
+ scriptInset = operator[](macroPos).nucleus()->asScriptInset();
+ macroInset = scriptInset->nuc()[0].nucleus()->asMacro();
+ }
+
+ // remove them from the MathData
+ erase(macroPos + 1, p);
+
+ // cursor outside this MathData?
+ if (thisSlice == -1)
+ return;
+
+ // fix cursor if right of p
+ if (thisPos >= int(p))
+ (*cur)[thisSlice].pos() -= p - (macroPos + 1);
+
+ // was the macro inset just inserted interactively and was now folded
+ // and the cursor is just behind?
+ if ((*cur)[thisSlice].pos() == int(macroPos + 1)
+ && interactiveInit
+ && fromInitToNormalMode
+ && macroInset->arity() > 0
+ && thisSlice + 1 == int(cur->depth())) {
+ // then enter it if the cursor was just behind
+ (*cur)[thisSlice].pos() = macroPos;
+ cur->push_back(CursorSlice(*macroInset));
+ macroInset->idxFirst(*cur);
+ }
+}
+
+
+void MathData::collectOptionalParameters(Cursor * cur,
+ const size_type numOptionalParams, vector<MathData> & params,
+ size_t & pos, MathAtom & scriptToPutAround,
+ const pos_type macroPos, const int thisPos, const int thisSlice)
+{
+ Buffer * buf = cur ? cur->buffer() : 0;