+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);
+ }
+ //lyxerr << "\nSums to: " << ar << "\n";
+}
+
+
+//
+// search differential stuff
+//
+
+// tests for 'd' or '\partial'
+bool testDiffItem(MathAtom const & at)
+{
+ return testString(at.nucleus(), "d");
+}
+
+
+bool testDiffArray(MathArray const & ar)
+{
+ return ar.size() && testDiffItem(ar.front());
+}
+
+
+bool testDiffFrac(MathInset * p)
+{
+ MathFracInset * f = p->asFracInset();
+ return f && testDiffArray(f->cell(0)) && testDiffArray(f->cell(1));
+}
+
+
+// is this something like ^number?
+bool extractDiffExponent(MathArray::iterator it, int & i)
+{
+ if (!(*it)->asScriptInset())
+ return false;
+
+ string s;
+ if (!extractString((*it).nucleus(), s))
+ return false;
+ istringstream is(s.c_str());
+ is >> i;
+ return is;
+}
+
+
+void extractDiff(MathArray & ar)
+{
+ //lyxerr << "\nDiffs from: " << ar << "\n";
+ for (MathArray::size_type i = 0; i < ar.size(); ++i) {
+ MathArray::iterator it = ar.begin() + i;
+
+ // is this a "differential fraction"?
+ if (!testDiffFrac(it->nucleus()))
+ continue;
+
+ MathFracInset * f = (*it)->asFracInset();
+ if (!f) {
+ lyxerr << "should not happen\n";
+ continue;
+ }
+
+ // create a proper diff inset
+ MathDiffInset * diff = new MathDiffInset;
+
+ // collect function, let jt point behind last used item
+ MathArray::iterator jt = it + 1;
+ //int n = 1;
+ MathArray & numer = f->cell(0);
+ if (numer.size() > 1 && numer.at(1)->asScriptInset()) {
+ // this is something like d^n f(x) / d... or d^n / d...
+ // FIXME
+ //n = 1;
+ if (numer.size() > 2)
+ diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
+ else
+ jt = extractArgument(diff->cell(0), jt, ar.end());
+ } else {
+ // simply d f(x) / d... or d/d...
+ if (numer.size() > 1)
+ diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
+ else
+ jt = extractArgument(diff->cell(0), jt, ar.end());
+ }
+
+ // collect denominator parts
+ MathArray & denom = f->cell(1);
+ for (MathArray::iterator dt = denom.begin(); dt != denom.end();) {
+ // find the next 'd'
+ MathArray::iterator et = find_if(dt + 1, denom.end(), &testDiffItem);
+
+ // point before this
+ MathArray::iterator st = et - 1;
+ MathScriptInset * script = (*st)->asScriptInset();
+ if (script && script->hasUp()) {
+ // things like d.../dx^n
+ int mult = 1;
+ if (extractNumber(script->up().data_, mult)) {
+ //lyxerr << "mult: " << mult << endl;
+ for (int i = 0; i < mult; ++i)
+ diff->addDer(MathArray(dt + 1, st));
+ }
+ } else {
+ // just d.../dx
+ diff->addDer(MathArray(dt + 1, et));
+ }
+ dt = et;
+ }
+
+ // cleanup
+ ar.erase(it + 1, jt);
+ (*it).reset(diff);
+ }
+ //lyxerr << "\nDiffs to: " << ar << "\n";
+}
+
+
+
+//
+// combine searches
+//
+