1 // This file contains most of the magic that extracts "context
2 // information" from the unstructered layout-oriented stuff in an
7 #include "math_amsarrayinset.h"
8 #include "math_arrayinset.h"
9 #include "math_charinset.h"
10 #include "math_deliminset.h"
11 #include "math_diffinset.h"
12 #include "math_exfuncinset.h"
13 #include "math_exintinset.h"
14 #include "math_fracinset.h"
15 #include "math_liminset.h"
16 #include "math_matrixinset.h"
17 #include "math_mathmlstream.h"
18 #include "math_numberinset.h"
19 #include "math_scriptinset.h"
20 #include "math_stringinset.h"
21 #include "math_symbolinset.h"
22 #include "math_unknowninset.h"
23 #include "math_parser.h"
26 #include "support/lyxlib.h"
27 #include "support/systemcall.h"
28 #include "support/filetools.h"
33 using std::istringstream;
38 ostream & operator<<(ostream & os, MathArray const & ar)
46 // define a function for tests
47 typedef bool TestItemFunc(MathAtom const &);
49 // define a function for replacing subexpressions
50 typedef MathInset * ReplaceArgumentFunc(const MathArray & ar);
54 // try to extract a super/subscript
55 // modify iterator position to point behind the thing
56 bool extractScript(MathArray & ar,
57 MathArray::iterator & pos, MathArray::iterator last)
59 // nothing to get here
63 // is this a scriptinset?
64 if (!(*pos)->asScriptInset())
67 // it is a scriptinset, use it.
74 // try to extract an "argument" to some function.
75 // returns position behind the argument
76 MathArray::iterator extractArgument(MathArray & ar,
77 MathArray::iterator pos, MathArray::iterator last, string const & = "")
79 // nothing to get here
83 // something deliminited _is_ an argument
84 if ((*pos)->asDelimInset()) {
89 // always take the first thing, no matter what it is
92 // go ahead if possible
97 // if the next item is a subscript, it most certainly belongs to the
99 extractScript(ar, pos, last);
103 // but it might be more than that.
104 // FIXME: not implemented
105 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
106 // // always take the first thing, no matter
108 // ar.push_back(*it);
116 // returns sequence of char with same code starting at it up to end
117 // it might be less, though...
119 (MathArray::const_iterator it, MathArray::const_iterator end)
122 for (; it != end && (*it)->asCharInset(); ++it)
123 s += (*it)->getChar();
128 void extractStrings(MathArray & ar)
130 //lyxerr << "\nStrings from: " << ar << "\n";
131 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
132 if (!ar[i]->asCharInset())
134 string s = charSequence(ar.begin() + i, ar.end());
135 ar[i] = MathAtom(new MathStringInset(s));
136 ar.erase(i + 1, i + s.size());
138 //lyxerr << "\nStrings to: " << ar << "\n";
142 MathInset const * singleItem(MathArray const & ar)
144 return ar.size() == 1 ? ar.begin()->nucleus() : 0;
148 void extractMatrices(MathArray & ar)
150 //lyxerr << "\nMatrices from: " << ar << "\n";
151 // first pass for explicitly delimited stuff
152 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
153 MathDelimInset const * del = (*it)->asDelimInset();
156 MathInset const * arr = singleItem(del->cell(0));
157 if (!arr || !arr->asGridInset())
159 *it = MathAtom(new MathMatrixInset(*(arr->asGridInset())));
162 // second pass for AMS "pmatrix" etc
163 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
164 MathAMSArrayInset const * ams = (*it)->asAMSArrayInset();
167 *it = MathAtom(new MathMatrixInset(*ams));
169 //lyxerr << "\nMatrices to: " << ar << "\n";
173 // convert this inset somehow to a string
174 bool extractString(MathAtom const & at, string & str)
177 str = string(1, at->getChar());
180 if (at->asStringInset()) {
181 str = at->asStringInset()->str();
188 // convert this inset somehow to a number
189 bool extractNumber(MathArray const & ar, int & i)
191 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
197 bool extractNumber(MathArray const & ar, double & d)
199 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
205 bool testString(MathAtom const & at, const string & str)
208 return extractString(at, s) && str == s;
212 // search end of nested sequence
213 MathArray::iterator endNestSearch(
214 MathArray::iterator it,
215 MathArray::iterator last,
216 TestItemFunc testOpen,
217 TestItemFunc testClose
220 for (int level = 0; it != last; ++it) {
232 // replace nested sequences by a real Insets
235 TestItemFunc testOpen,
236 TestItemFunc testClose,
237 ReplaceArgumentFunc replaceArg
240 // use indices rather than iterators for the loop because we are going
241 // to modify the array.
242 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
243 // check whether this is the begin of the sequence
244 MathArray::iterator it = ar.begin() + i;
248 // search end of sequence
249 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
253 // create a proper inset as replacement
254 MathInset * p = replaceArg(MathArray(it + 1, jt));
256 // replace the original stuff by the new inset
257 ar.erase(it + 1, jt + 1);
265 // split scripts into seperate super- and subscript insets. sub goes in
269 void splitScripts(MathArray & ar)
271 //lyxerr << "\nScripts from: " << ar << "\n";
272 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
273 MathArray::iterator it = ar.begin() + i;
275 // is this script inset?
276 MathScriptInset * p = (*it).nucleus()->asScriptInset();
280 // no problem if we don't have both...
281 if (!p->hasUp() || !p->hasDown())
284 // create extra script inset and move superscript over
285 MathScriptInset * q = new MathScriptInset;
287 std::swap(q->up(), p->up());
288 p->removeScript(true);
290 // insert new inset behind
292 ar.insert(i, MathAtom(q));
294 //lyxerr << "\nScripts to: " << ar << "\n";
302 void extractExps(MathArray & ar)
304 //lyxerr << "\nExps from: " << ar << "\n";
306 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
307 MathArray::iterator it = ar.begin() + i;
310 MathCharInset const * p = (*it)->asCharInset();
311 if (!p || p->getChar() != 'e')
314 // we need an exponent but no subscript
315 MathScriptInset const * sup = (*(it + 1))->asScriptInset();
316 if (!sup || sup->hasDown())
319 // create a proper exp-inset as replacement
320 *it = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
323 //lyxerr << "\nExps to: " << ar << "\n";
328 // extract det(...) from |matrix|
330 void extractDets(MathArray & ar)
332 //lyxerr << "\ndet from: " << ar << "\n";
333 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
334 MathDelimInset const * del = (*it)->asDelimInset();
339 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
341 //lyxerr << "\ndet to: " << ar << "\n";
349 bool isDigitOrSimilar(char c)
351 return ('0' <= c && c <= '9') || c == '.';
355 // returns sequence of digits
357 (MathArray::const_iterator it, MathArray::const_iterator end)
360 for (; it != end && (*it)->asCharInset(); ++it) {
361 if (!isDigitOrSimilar((*it)->getChar()))
363 s += (*it)->getChar();
369 void extractNumbers(MathArray & ar)
371 //lyxerr << "\nNumbers from: " << ar << "\n";
372 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
373 if (!ar[i]->asCharInset())
375 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
378 string s = digitSequence(ar.begin() + i, ar.end());
380 ar[i] = MathAtom(new MathNumberInset(s));
381 ar.erase(i + 1, i + s.size());
383 //lyxerr << "\nNumbers to: " << ar << "\n";
389 // search deliminiters
392 bool testOpenParan(MathAtom const & at)
394 return testString(at, "(");
398 bool testCloseParan(MathAtom const & at)
400 return testString(at, ")");
404 MathInset * replaceDelims(const MathArray & ar)
406 return new MathDelimInset("(", ")", ar);
410 // replace '('...')' sequences by a real MathDelimInset
411 void extractDelims(MathArray & ar)
413 //lyxerr << "\nDelims from: " << ar << "\n";
414 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
415 //lyxerr << "\nDelims to: " << ar << "\n";
421 // search well-known functions
425 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
426 // assume 'extractDelims' ran before
427 void extractFunctions(MathArray & ar)
429 // we need at least two items...
433 //lyxerr << "\nFunctions from: " << ar << "\n";
434 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
435 MathArray::iterator it = ar.begin() + i;
436 MathArray::iterator jt = it + 1;
440 if ((*it)->asUnknownInset()) {
441 // it certainly is if it is well known...
442 name = (*it)->name();
444 // is this a user defined function?
445 // it it probably not, if it doesn't have a name.
446 if (!extractString(*it, name))
448 // it is not if it has no argument
451 // guess so, if this is followed by
452 // a DelimInset with a single item in the cell
453 MathDelimInset const * del = (*jt)->asDelimInset();
454 if (!del || del->cell(0).size() != 1)
456 // fall trough into main branch
459 // do we have an exponent like in
460 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
462 extractScript(exp, jt, ar.end());
464 // create a proper inset as replacement
465 MathExFuncInset * p = new MathExFuncInset(name);
467 // jt points to the "argument". Get hold of this.
468 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
470 // replace the function name by a real function inset
473 // remove the source of the argument from the array
474 ar.erase(it + 1, st);
476 // re-insert exponent
477 ar.insert(i + 1, exp);
478 //lyxerr << "\nFunctions to: " << ar << "\n";
487 bool testSymbol(MathAtom const & at, string const & name)
489 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
493 bool testIntSymbol(MathAtom const & at)
495 return testSymbol(at, "int");
499 bool testIntegral(MathAtom const & at)
503 ( at->asScriptInset()
504 && at->asScriptInset()->nuc().size()
505 && testIntSymbol(at->asScriptInset()->nuc().back()) );
510 bool testIntDiff(MathAtom const & at)
512 return testString(at, "d");
516 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
517 // assume 'extractDelims' ran before
518 void extractIntegrals(MathArray & ar)
520 // we need at least three items...
524 //lyxerr << "\nIntegrals from: " << ar << "\n";
525 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
526 MathArray::iterator it = ar.begin() + i;
529 MathArray::iterator jt =
530 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
532 // something sensible found?
536 // is this a integral name?
537 if (!testIntegral(*it))
540 // core ist part from behind the scripts to the 'd'
541 MathExIntInset * p = new MathExIntInset("int");
543 // handle scripts if available
544 if (!testIntSymbol(*it)) {
545 p->cell(2) = (*it)->asScriptInset()->down();
546 p->cell(3) = (*it)->asScriptInset()->up();
548 p->cell(0) = MathArray(it + 1, jt);
550 // use the "thing" behind the 'd' as differential
551 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
554 ar.erase(it + 1, tt);
557 //lyxerr << "\nIntegrals to: " << ar << "\n";
566 bool testEqualSign(MathAtom const & at)
568 return testString(at, "=");
572 bool testSumSymbol(MathAtom const & p)
574 return testSymbol(p, "sum");
578 bool testSum(MathAtom const & at)
582 ( at->asScriptInset()
583 && at->asScriptInset()->nuc().size()
584 && testSumSymbol(at->asScriptInset()->nuc().back()) );
588 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
589 // assume 'extractDelims' ran before
590 void extractSums(MathArray & ar)
592 // we need at least two items...
596 //lyxerr << "\nSums from: " << ar << "\n";
597 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
598 MathArray::iterator it = ar.begin() + i;
600 // is this a sum name?
604 // create a proper inset as replacement
605 MathExIntInset * p = new MathExIntInset("sum");
607 // collect lower bound and summation index
608 MathScriptInset const * sub = (*it)->asScriptInset();
609 if (sub && sub->hasDown()) {
610 // try to figure out the summation index from the subscript
611 MathArray const & ar = sub->down();
612 MathArray::const_iterator xt =
613 find_if(ar.begin(), ar.end(), &testEqualSign);
614 if (xt != ar.end()) {
615 // we found a '=', use everything in front of that as index,
616 // and everything behind as lower index
617 p->cell(1) = MathArray(ar.begin(), xt);
618 p->cell(2) = MathArray(xt + 1, ar.end());
620 // use everything as summation index, don't use scripts.
625 // collect upper bound
626 if (sub && sub->hasUp())
627 p->cell(3) = sub->up();
629 // use something behind the script as core
630 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
633 ar.erase(it + 1, tt);
636 //lyxerr << "\nSums to: " << ar << "\n";
641 // search differential stuff
644 // tests for 'd' or '\partial'
645 bool testDiffItem(MathAtom const & at)
647 return testString(at, "d");
651 bool testDiffArray(MathArray const & ar)
653 return ar.size() && testDiffItem(ar.front());
657 bool testDiffFrac(MathAtom const & at)
661 && testDiffArray(at->asFracInset()->cell(0))
662 && testDiffArray(at->asFracInset()->cell(1));
666 void extractDiff(MathArray & ar)
668 //lyxerr << "\nDiffs from: " << ar << "\n";
669 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
670 MathArray::iterator it = ar.begin() + i;
672 // is this a "differential fraction"?
673 if (!testDiffFrac(*it))
676 MathFracInset const * f = (*it)->asFracInset();
678 lyxerr << "should not happen\n";
682 // create a proper diff inset
683 MathDiffInset * diff = new MathDiffInset;
685 // collect function, let jt point behind last used item
686 MathArray::iterator jt = it + 1;
688 MathArray const & numer = f->cell(0);
689 if (numer.size() > 1 && numer[1]->asScriptInset()) {
690 // this is something like d^n f(x) / d... or d^n / d...
693 if (numer.size() > 2)
694 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
696 jt = extractArgument(diff->cell(0), jt, ar.end());
698 // simply d f(x) / d... or d/d...
699 if (numer.size() > 1)
700 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
702 jt = extractArgument(diff->cell(0), jt, ar.end());
705 // collect denominator parts
706 MathArray const & denom = f->cell(1);
707 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
709 MathArray::const_iterator et
710 = find_if(dt + 1, denom.end(), &testDiffItem);
713 MathArray::const_iterator st = et - 1;
714 MathScriptInset const * script = (*st)->asScriptInset();
715 if (script && script->hasUp()) {
716 // things like d.../dx^n
718 if (extractNumber(script->up(), mult)) {
719 //lyxerr << "mult: " << mult << endl;
720 for (int i = 0; i < mult; ++i)
721 diff->addDer(MathArray(dt + 1, st));
725 diff->addDer(MathArray(dt + 1, et));
731 ar.erase(it + 1, jt);
732 *it = MathAtom(diff);
734 //lyxerr << "\nDiffs to: " << ar << "\n";
743 bool testRightArrow(MathAtom const & at)
745 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
750 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
751 // assume 'extractDelims' ran before
752 void extractLims(MathArray & ar)
754 // we need at least three items...
758 //lyxerr << "\nLimits from: " << ar << "\n";
759 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
760 MathArray::iterator it = ar.begin() + i;
762 // is this a limit function?
763 if (!testSymbol(*it, "lim"))
766 // the next one must be a subscript (without superscript)
767 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
768 if (!sub || !sub->hasDown() || sub->hasUp())
771 // and it must contain a -> symbol
772 MathArray const & s = sub->down();
773 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
777 // the -> splits the subscript int x and x0
778 MathArray x = MathArray(s.begin(), st);
779 MathArray x0 = MathArray(st + 1, s.end());
781 // use something behind the script as core
783 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
786 ar.erase(it + 1, tt);
788 // create a proper inset as replacement
789 *it = MathAtom(new MathLimInset(f, x, x0));
791 //lyxerr << "\nLimits to: " << ar << "\n";
799 void extractStructure(MathArray & ar)
801 extractIntegrals(ar);
807 extractFunctions(ar);
816 void write(MathArray const & dat, WriteStream & wi)
820 wi.firstitem() = true;
821 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
823 wi.firstitem() = false;
828 void normalize(MathArray const & ar, NormalStream & os)
830 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
831 (*it)->normalize(os);
835 void octavize(MathArray const & dat, OctaveStream & os)
838 extractStructure(ar);
839 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
844 void maplize(MathArray const & dat, MapleStream & os)
847 extractStructure(ar);
848 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
853 void mathematicize(MathArray const & dat, MathematicaStream & os)
856 extractStructure(ar);
857 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
858 (*it)->mathematicize(os);
862 void mathmlize(MathArray const & dat, MathMLStream & os)
865 extractStructure(ar);
868 else if (ar.size() == 1)
872 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
873 (*it)->mathmlize(os);
883 string captureOutput(string const & cmd, string const & data)
885 string outfile = lyx::tempName(string(), "mathextern");
886 string full = "echo '" + data + "' | (" + cmd + ") > " + outfile;
887 lyxerr << "calling: " << full << endl;
889 dummy.startscript(Systemcall::Wait, full);
890 string out = GetFileContents(outfile);
891 lyx::unlink(outfile);
892 lyxerr << "result: '" << out << "'" << endl;
897 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
899 string header = "readlib(latex):\n";
901 // remove the \\it for variable names
902 //"#`latex/csname_font` := `\\it `:"
904 "`latex/csname_font` := ``:\n";
906 // export matrices in (...) instead of [...]
908 "`latex/latex/matrix` := "
909 "subs(`[`=`(`, `]`=`)`,"
910 "eval(`latex/latex/matrix`)):\n";
912 // replace \\cdots with proper '*'
914 "`latex/latex/*` := "
915 "subs(`\\,`=`\\cdot `,"
916 "eval(`latex/latex/*`)):\n";
918 // remove spurious \\noalign{\\medskip} in matrix output
920 "`latex/latex/matrix`:= "
921 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
922 "eval(`latex/latex/matrix`)):\n";
924 //"#`latex/latex/symbol` "
925 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
927 string trailer = "quit;";
931 string expr = os.str().c_str();
932 lyxerr << "ar: '" << ar << "'\n";
933 lyxerr << "ms: '" << os.str() << "'\n";
935 for (int i = 0; i < 100; ++i) { // at most 100 attempts
936 // try to fix missing '*' the hard way by using mint
938 // ... > echo "1A;" | mint -i 1 -S -s -q
941 // Probably missing an operator such as * p
943 lyxerr << "checking expr: '" << expr << "'\n";
944 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ";");
946 break; // expression syntax is ok
947 istringstream is(out.c_str());
950 if (line.find("on line") != 0)
951 break; // error message not identified
953 string::size_type pos = line.find('^');
954 if (pos == string::npos || pos < 15)
955 break; // caret position not found
956 pos -= 15; // skip the "on line ..." part
957 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
958 break; // two '*' in a row are definitely bad
959 expr.insert(pos, "*");
962 string full = "latex(" + extra + '(' + expr + "));";
963 string out = captureOutput("maple -q", header + full + trailer);
969 mathed_parse_cell(res, out);
974 MathArray pipeThroughOctave(string const &, MathArray const & ar)
979 string expr = os.str().c_str();
982 lyxerr << "pipe: ar: '" << ar << "'\n";
983 lyxerr << "pipe: expr: '" << expr << "'\n";
985 for (int i = 0; i < 100; ++i) { // at most 100 attempts
987 // try to fix missing '*' the hard way
989 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
992 lyxerr << "checking expr: '" << expr << "'\n";
993 out = captureOutput("octave -q 2>&1", expr);
994 lyxerr << "checking out: '" << out << "'\n";
996 // leave loop if expression syntax is probably ok
997 if (out.find("parse error:") == string::npos)
1000 // search line with single caret
1001 istringstream is(out.c_str());
1005 lyxerr << "skipping line: '" << line << "'\n";
1006 if (line.find(">>> ") != string::npos)
1010 // found line with error, next line is the one with caret
1012 string::size_type pos = line.find('^');
1013 lyxerr << "caret line: '" << line << "'\n";
1014 lyxerr << "found caret at pos: '" << pos << "'\n";
1015 if (pos == string::npos || pos < 4)
1016 break; // caret position not found
1017 pos -= 4; // skip the ">>> " part
1018 if (expr[pos] == '*')
1019 break; // two '*' in a row are definitely bad
1020 expr.insert(pos, "*");
1027 out = out.substr(6);
1029 // parse output as matrix or single number
1030 MathAtom at(new MathArrayInset("array", out));
1031 MathArrayInset const * mat = at.nucleus()->asArrayInset();
1033 if (mat->ncols() == 1 && mat->nrows() == 1)
1034 res.append(mat->cell(0));
1036 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1037 res.back().nucleus()->cell(0).push_back(at);
1045 MathArray pipeThroughExtern(string const & lang, string const & extra,
1046 MathArray const & ar)
1048 if (lang == "octave")
1049 return pipeThroughOctave(extra, ar);
1051 if (lang == "maple")
1052 return pipeThroughMaple(extra, ar);
1054 // create normalized expression
1056 NormalStream ns(os);
1057 os << "[" << extra << ' ';
1060 string data = os.str().c_str();
1062 // search external script
1063 string file = LibFileSearch("mathed", "extern_" + lang);
1065 lyxerr << "converter to '" << lang << "' not found\n";
1069 // run external sript
1070 string out = captureOutput(file, data);
1072 mathed_parse_cell(res, out);