+ ar.erase(it + 1, st);
+
+ // re-insert exponent
+ ar.insert(i + 1, exp);
+ //lyxerr << "\nFunctions to: " << ar << "\n";
+ }
+}
+
+
+//
+// search integrals
+//
+
+bool testSymbol(MathInset * p, string const & name)
+{
+ return p->asSymbolInset() && p->asSymbolInset()->name() == name;
+}
+
+
+bool testIntSymbol(MathInset * p)
+{
+ return testSymbol(p, "int");
+}
+
+
+bool testIntDiff(MathInset * p)
+{
+ return testString(p, "d");
+}
+
+
+// replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
+// assume 'extractDelims' ran before
+void extractIntegrals(MathArray & ar)
+{
+ // we need at least three items...
+ if (ar.size() <= 2)
+ return;
+
+ //lyxerr << "\nIntegrals from: " << ar << "\n";
+ for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
+ MathArray::iterator it = ar.begin() + i;
+
+ // is this a integral name?
+ if (!testIntSymbol(it->nucleus()))
+ continue;
+
+ // search 'd'
+ MathArray::iterator jt =
+ endNestSearch(it, ar.end(), testIntSymbol, testIntDiff);
+
+ // something sensible found?
+ if (jt == ar.end())
+ continue;
+
+ // create a proper inset as replacement
+ MathExIntInset * p = new MathExIntInset("int");
+
+ // collect subscript if any
+ MathArray::iterator st = it + 1;
+ if (st != ar.end())
+ if (MathScriptInset * sub = (*st)->asScriptInset())
+ if (sub->hasDown()) {
+ p->cell(2) = sub->down().data_;
+ ++st;
+ }
+
+ // collect superscript if any
+ if (st != ar.end())
+ if (MathScriptInset * sup = (*st)->asScriptInset())
+ if (sup->hasUp()) {
+ p->cell(3) = sup->up().data_;
+ ++st;
+ }
+
+ // core ist part from behind the scripts to the 'd'
+ p->cell(0) = MathArray(st, jt);
+
+ // use the "thing" behind the 'd' as differential
+ MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
+
+ // remove used parts
+ ar.erase(it + 1, tt);
+ (*it).reset(p);
+ }
+ //lyxerr << "\nIntegrals to: " << ar << "\n";
+}
+
+
+//
+// search sums
+//
+
+bool testSumSymbol(MathInset * p)
+{
+ return testSymbol(p, "sum");
+}
+
+
+bool testEqualSign(MathAtom const & at)
+{
+ return testString(at.nucleus(), "=");
+}
+
+
+
+// replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
+// assume 'extractDelims' ran before
+void extractSums(MathArray & ar)
+{
+ // we need at least two items...
+ if (ar.size() <= 1)
+ return;
+
+ //lyxerr << "\nSums from: " << ar << "\n";
+ for (MathArray::size_type i = 0; i + 1< ar.size(); ++i) {
+ MathArray::iterator it = ar.begin() + i;
+
+ // is this a sum name?
+ if (!testSumSymbol(it->nucleus()))
+ continue;
+
+ // create a proper inset as replacement
+ MathExIntInset * p = new MathExIntInset("sum");
+
+ // collect lower bound and summation index
+ MathArray::iterator st = it + 1;
+ if (st != ar.end())
+ if (MathScriptInset * sub = (*st)->asScriptInset())
+ if (sub->hasDown()) {
+ // try to figure out the summation index from the subscript
+ MathArray & ar = sub->down().data_;
+ MathArray::iterator it =
+ find_if(ar.begin(), ar.end(), &testEqualSign);
+ if (it != ar.end()) {
+ // we found a '=', use everything in front of that as index,
+ // and everything behind as lower index
+ p->cell(1) = MathArray(ar.begin(), it);
+ p->cell(2) = MathArray(it + 1, ar.end());
+ } else {
+ // use everything as summation index, don't use scripts.
+ p->cell(1) = ar;
+ }
+ ++st;
+ }
+
+ // collect upper bound
+ if (st != ar.end())
+ if (MathScriptInset * sup = (*st)->asScriptInset())
+ if (sup->hasUp()) {
+ p->cell(3) = sup->up().data_;
+ ++st;
+ }
+
+ // use some behind the script as core
+ MathArray::iterator tt = extractArgument(p->cell(0), st, ar.end());
+
+ // cleanup
+ ar.erase(it + 1, tt);
+ (*it).reset(p);