3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
11 // This file contains most of the magic that extracts "context
12 // information" from the unstructered layout-oriented stuff in an
17 #include "math_extern.h"
18 #include "math_arrayinset.h"
19 #include "math_charinset.h"
20 #include "math_deliminset.h"
21 #include "math_diffinset.h"
22 #include "math_exfuncinset.h"
23 #include "math_exintinset.h"
24 #include "math_fracinset.h"
25 #include "math_liminset.h"
26 #include "math_matrixinset.h"
27 #include "math_mathmlstream.h"
28 #include "math_numberinset.h"
29 #include "math_scriptinset.h"
30 #include "math_stringinset.h"
31 #include "math_symbolinset.h"
32 #include "math_parser.h"
33 #include "support/std_sstream.h"
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
40 using namespace lyx::support;
43 using std::istringstream;
44 using std::ostringstream;
49 ostream & operator<<(ostream & os, MathArray const & ar)
57 // define a function for tests
58 typedef bool TestItemFunc(MathAtom const &);
60 // define a function for replacing subexpressions
61 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
65 // try to extract a super/subscript
66 // modify iterator position to point behind the thing
67 bool extractScript(MathArray & ar,
68 MathArray::iterator & pos, MathArray::iterator last)
70 // nothing to get here
74 // is this a scriptinset?
75 if (!(*pos)->asScriptInset())
78 // it is a scriptinset, use it.
85 // try to extract an "argument" to some function.
86 // returns position behind the argument
87 MathArray::iterator extractArgument(MathArray & ar,
88 MathArray::iterator pos, MathArray::iterator last, string const & = "")
90 // nothing to get here
94 // something deliminited _is_ an argument
95 if ((*pos)->asDelimInset()) {
100 // always take the first thing, no matter what it is
103 // go ahead if possible
108 // if the next item is a subscript, it most certainly belongs to the
110 extractScript(ar, pos, last);
114 // but it might be more than that.
115 // FIXME: not implemented
116 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
117 // // always take the first thing, no matter
119 // ar.push_back(*it);
127 // returns sequence of char with same code starting at it up to end
128 // it might be less, though...
130 (MathArray::const_iterator it, MathArray::const_iterator end)
133 for (; it != end && (*it)->asCharInset(); ++it)
134 s += (*it)->getChar();
139 void extractStrings(MathArray & ar)
141 //lyxerr << "\nStrings from: " << ar << endl;
142 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
143 if (!ar[i]->asCharInset())
145 string s = charSequence(ar.begin() + i, ar.end());
146 ar[i] = MathAtom(new MathStringInset(s));
147 ar.erase(i + 1, i + s.size());
149 //lyxerr << "\nStrings to: " << ar << endl;
153 void extractMatrices(MathArray & ar)
155 //lyxerr << "\nMatrices from: " << ar << endl;
156 // first pass for explicitly delimited stuff
157 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
158 if (!ar[i]->asDelimInset())
160 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
163 if (!arr.front()->asGridInset())
165 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
168 // second pass for AMS "pmatrix" etc
169 for (MathArray::size_type i = 0; i < ar.size(); ++i)
170 if (ar[i]->asAMSArrayInset())
171 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
172 //lyxerr << "\nMatrices to: " << ar << endl;
176 // convert this inset somehow to a string
177 bool extractString(MathAtom const & at, string & str)
180 str = string(1, at->getChar());
183 if (at->asStringInset()) {
184 str = at->asStringInset()->str();
191 // convert this inset somehow to a number
192 bool extractNumber(MathArray const & ar, int & i)
194 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
200 bool extractNumber(MathArray const & ar, double & d)
202 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
208 bool testString(MathAtom const & at, string const & str)
211 return extractString(at, s) && str == s;
215 // search end of nested sequence
216 MathArray::iterator endNestSearch(
217 MathArray::iterator it,
218 MathArray::iterator last,
219 TestItemFunc testOpen,
220 TestItemFunc testClose
223 for (int level = 0; it != last; ++it) {
235 // replace nested sequences by a real Insets
238 TestItemFunc testOpen,
239 TestItemFunc testClose,
240 ReplaceArgumentFunc replaceArg
243 // use indices rather than iterators for the loop because we are going
244 // to modify the array.
245 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
246 // check whether this is the begin of the sequence
247 if (!testOpen(ar[i]))
250 // search end of sequence
251 MathArray::iterator it = ar.begin() + i;
252 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
256 // replace the original stuff by the new inset
257 ar[i] = replaceArg(MathArray(it + 1, jt));
258 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 << endl;
272 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
273 // is this script inset?
274 if (!ar[i]->asScriptInset())
277 // no problem if we don't have both...
278 if (!ar[i]->asScriptInset()->hasUp())
280 if (!ar[i]->asScriptInset()->hasDown())
283 // create extra script inset and move superscript over
284 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
285 MathScriptInset * q = new MathScriptInset(true);
286 std::swap(q->up(), p->up());
287 p->removeScript(true);
289 // insert new inset behind
291 ar.insert(i, MathAtom(q));
293 //lyxerr << "\nScripts to: " << ar << endl;
301 void extractExps(MathArray & ar)
303 //lyxerr << "\nExps from: " << ar << endl;
304 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
306 if (ar[i]->getChar() != 'e')
309 // we need an exponent but no subscript
310 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
311 if (!sup || sup->hasDown())
314 // create a proper exp-inset as replacement
315 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
318 //lyxerr << "\nExps to: " << ar << endl;
323 // extract det(...) from |matrix|
325 void extractDets(MathArray & ar)
327 //lyxerr << "\ndet from: " << ar << endl;
328 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
329 MathDelimInset const * del = (*it)->asDelimInset();
334 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
336 //lyxerr << "\ndet to: " << ar << endl;
344 bool isDigitOrSimilar(char c)
346 return ('0' <= c && c <= '9') || c == '.';
350 // returns sequence of digits
352 (MathArray::const_iterator it, MathArray::const_iterator end)
355 for (; it != end && (*it)->asCharInset(); ++it) {
356 if (!isDigitOrSimilar((*it)->getChar()))
358 s += (*it)->getChar();
364 void extractNumbers(MathArray & ar)
366 //lyxerr << "\nNumbers from: " << ar << endl;
367 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
368 if (!ar[i]->asCharInset())
370 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
373 string s = digitSequence(ar.begin() + i, ar.end());
375 ar[i] = MathAtom(new MathNumberInset(s));
376 ar.erase(i + 1, i + s.size());
378 //lyxerr << "\nNumbers to: " << ar << endl;
384 // search deliminiters
387 bool testOpenParan(MathAtom const & at)
389 return testString(at, "(");
393 bool testCloseParan(MathAtom const & at)
395 return testString(at, ")");
399 MathAtom replaceDelims(const MathArray & ar)
401 return MathAtom(new MathDelimInset("(", ")", ar));
405 // replace '('...')' sequences by a real MathDelimInset
406 void extractDelims(MathArray & ar)
408 //lyxerr << "\nDelims from: " << ar << endl;
409 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
410 //lyxerr << "\nDelims to: " << ar << endl;
416 // search well-known functions
420 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
421 // assume 'extractDelims' ran before
422 void extractFunctions(MathArray & ar)
424 // we need at least two items...
428 //lyxerr << "\nFunctions from: " << ar << endl;
429 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
430 MathArray::iterator it = ar.begin() + i;
431 MathArray::iterator jt = it + 1;
435 if ((*it)->asUnknownInset()) {
436 // it certainly is if it is well known...
437 name = (*it)->name();
439 // is this a user defined function?
440 // it it probably not, if it doesn't have a name.
441 if (!extractString(*it, name))
443 // it is not if it has no argument
446 // guess so, if this is followed by
447 // a DelimInset with a single item in the cell
448 MathDelimInset const * del = (*jt)->asDelimInset();
449 if (!del || del->cell(0).size() != 1)
451 // fall trough into main branch
454 // do we have an exponent like in
455 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
457 extractScript(exp, jt, ar.end());
459 // create a proper inset as replacement
460 MathExFuncInset * p = new MathExFuncInset(name);
462 // jt points to the "argument". Get hold of this.
463 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
465 // replace the function name by a real function inset
468 // remove the source of the argument from the array
469 ar.erase(it + 1, st);
471 // re-insert exponent
472 ar.insert(i + 1, exp);
473 //lyxerr << "\nFunctions to: " << ar << endl;
482 bool testSymbol(MathAtom const & at, string const & name)
484 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
488 bool testIntSymbol(MathAtom const & at)
490 return testSymbol(at, "int");
494 bool testIntegral(MathAtom const & at)
498 ( at->asScriptInset()
499 && at->asScriptInset()->nuc().size()
500 && testIntSymbol(at->asScriptInset()->nuc().back()) );
505 bool testIntDiff(MathAtom const & at)
507 return testString(at, "d");
511 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
512 // assume 'extractDelims' ran before
513 void extractIntegrals(MathArray & ar)
515 // we need at least three items...
519 //lyxerr << "\nIntegrals from: " << ar << endl;
520 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
521 MathArray::iterator it = ar.begin() + i;
524 MathArray::iterator jt =
525 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
527 // something sensible found?
531 // is this a integral name?
532 if (!testIntegral(*it))
535 // core ist part from behind the scripts to the 'd'
536 MathExIntInset * p = new MathExIntInset("int");
538 // handle scripts if available
539 if (!testIntSymbol(*it)) {
540 p->cell(2) = (*it)->asScriptInset()->down();
541 p->cell(3) = (*it)->asScriptInset()->up();
543 p->cell(0) = MathArray(it + 1, jt);
545 // use the "thing" behind the 'd' as differential
546 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
549 ar.erase(it + 1, tt);
552 //lyxerr << "\nIntegrals to: " << ar << endl;
561 bool testEqualSign(MathAtom const & at)
563 return testString(at, "=");
567 bool testSumSymbol(MathAtom const & p)
569 return testSymbol(p, "sum");
573 bool testSum(MathAtom const & at)
577 ( at->asScriptInset()
578 && at->asScriptInset()->nuc().size()
579 && testSumSymbol(at->asScriptInset()->nuc().back()) );
583 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
584 // assume 'extractDelims' ran before
585 void extractSums(MathArray & ar)
587 // we need at least two items...
591 //lyxerr << "\nSums from: " << ar << endl;
592 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
593 MathArray::iterator it = ar.begin() + i;
595 // is this a sum name?
599 // create a proper inset as replacement
600 MathExIntInset * p = new MathExIntInset("sum");
602 // collect lower bound and summation index
603 MathScriptInset const * sub = ar[i]->asScriptInset();
604 if (sub && sub->hasDown()) {
605 // try to figure out the summation index from the subscript
606 MathArray const & ar = sub->down();
607 MathArray::const_iterator xt =
608 find_if(ar.begin(), ar.end(), &testEqualSign);
609 if (xt != ar.end()) {
610 // we found a '=', use everything in front of that as index,
611 // and everything behind as lower index
612 p->cell(1) = MathArray(ar.begin(), xt);
613 p->cell(2) = MathArray(xt + 1, ar.end());
615 // use everything as summation index, don't use scripts.
620 // collect upper bound
621 if (sub && sub->hasUp())
622 p->cell(3) = sub->up();
624 // use something behind the script as core
625 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
628 ar.erase(it + 1, tt);
631 //lyxerr << "\nSums to: " << ar << endl;
636 // search differential stuff
639 // tests for 'd' or '\partial'
640 bool testDiffItem(MathAtom const & at)
642 return testString(at, "d");
646 bool testDiffArray(MathArray const & ar)
648 return ar.size() && testDiffItem(ar.front());
652 bool testDiffFrac(MathAtom const & at)
656 && testDiffArray(at->asFracInset()->cell(0))
657 && testDiffArray(at->asFracInset()->cell(1));
661 void extractDiff(MathArray & ar)
663 //lyxerr << "\nDiffs from: " << ar << endl;
664 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
665 MathArray::iterator it = ar.begin() + i;
667 // is this a "differential fraction"?
668 if (!testDiffFrac(*it))
671 MathFracInset const * f = (*it)->asFracInset();
673 lyxerr << "should not happen" << endl;
677 // create a proper diff inset
678 MathDiffInset * diff = new MathDiffInset;
680 // collect function, let jt point behind last used item
681 MathArray::iterator jt = it + 1;
683 MathArray const & numer = f->cell(0);
684 if (numer.size() > 1 && numer[1]->asScriptInset()) {
685 // this is something like d^n f(x) / d... or d^n / d...
688 if (numer.size() > 2)
689 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
691 jt = extractArgument(diff->cell(0), jt, ar.end());
693 // simply d f(x) / d... or d/d...
694 if (numer.size() > 1)
695 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
697 jt = extractArgument(diff->cell(0), jt, ar.end());
700 // collect denominator parts
701 MathArray const & denom = f->cell(1);
702 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
704 MathArray::const_iterator et
705 = find_if(dt + 1, denom.end(), &testDiffItem);
708 MathArray::const_iterator st = et - 1;
709 MathScriptInset const * script = (*st)->asScriptInset();
710 if (script && script->hasUp()) {
711 // things like d.../dx^n
713 if (extractNumber(script->up(), mult)) {
714 //lyxerr << "mult: " << mult << endl;
715 for (int i = 0; i < mult; ++i)
716 diff->addDer(MathArray(dt + 1, st));
720 diff->addDer(MathArray(dt + 1, et));
726 ar.erase(it + 1, jt);
727 *it = MathAtom(diff);
729 //lyxerr << "\nDiffs to: " << ar << endl;
738 bool testRightArrow(MathAtom const & at)
740 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
745 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
746 // assume 'extractDelims' ran before
747 void extractLims(MathArray & ar)
749 // we need at least three items...
753 //lyxerr << "\nLimits from: " << ar << endl;
754 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
755 MathArray::iterator it = ar.begin() + i;
757 // is this a limit function?
758 if (!testSymbol(*it, "lim"))
761 // the next one must be a subscript (without superscript)
762 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
763 if (!sub || !sub->hasDown() || sub->hasUp())
766 // and it must contain a -> symbol
767 MathArray const & s = sub->down();
768 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
772 // the -> splits the subscript int x and x0
773 MathArray x = MathArray(s.begin(), st);
774 MathArray x0 = MathArray(st + 1, s.end());
776 // use something behind the script as core
778 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
781 ar.erase(it + 1, tt);
783 // create a proper inset as replacement
784 *it = MathAtom(new MathLimInset(f, x, x0));
786 //lyxerr << "\nLimits to: " << ar << endl;
794 void extractStructure(MathArray & ar)
796 //lyxerr << "\nStructure from: " << ar << endl;
797 extractIntegrals(ar);
803 extractFunctions(ar);
809 //lyxerr << "\nStructure to: " << ar << endl;
813 void write(MathArray const & dat, WriteStream & wi)
817 wi.firstitem() = true;
818 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
820 wi.firstitem() = false;
825 void normalize(MathArray const & ar, NormalStream & os)
827 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
828 (*it)->normalize(os);
832 void octave(MathArray const & dat, OctaveStream & os)
835 extractStructure(ar);
836 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
841 void maple(MathArray const & dat, MapleStream & os)
844 extractStructure(ar);
845 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
850 void maxima(MathArray const & dat, MaximaStream & os)
853 extractStructure(ar);
854 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
859 void mathematica(MathArray const & dat, MathematicaStream & os)
862 extractStructure(ar);
863 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
864 (*it)->mathematica(os);
868 void mathmlize(MathArray const & dat, MathMLStream & os)
871 extractStructure(ar);
874 else if (ar.size() == 1)
878 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
879 (*it)->mathmlize(os);
889 string captureOutput(string const & cmd, string const & data)
891 string command = "echo '" + data + "' | " + cmd;
892 lyxerr << "calling: " << command << endl;
893 cmd_ret const ret = RunCommand(command);
897 string::size_type get_matching_brace(string const & str, string::size_type i)
900 string::size_type n = str.size();
902 i = str.find_first_of("{}", i+1);
903 if (i == string::npos) return i;
914 string::size_type get_matching_brace_back(string const & str, string::size_type i)
918 i = str.find_last_of("{}", i-1);
919 if (i == string::npos) return i;
930 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
935 string expr = STRCONV(os.str());
936 string const header = "SIMPSUM:true;";
939 for (int i = 0; i < 100; ++i) { // at most 100 attempts
940 // try to fix missing '*' the hard way
942 // > echo "2x;" | maxima
944 // (C1) Incorrect syntax: x is not an infix operator
948 lyxerr << "checking expr: '" << expr << "'" << endl;
949 string full = header + "tex(" + expr + ");";
950 out = captureOutput("maxima", full);
952 // leave loop if expression syntax is probably ok
953 if (out.find("Incorrect syntax") == string::npos)
956 // search line with "Incorrect syntax"
957 istringstream is(out.c_str());
961 if (line.find("Incorrect syntax") != string::npos)
965 // 2nd next line is the one with caret
968 string::size_type pos = line.find('^');
969 lyxerr << "found caret at pos: '" << pos << "'" << endl;
970 if (pos == string::npos || pos < 4)
971 break; // caret position not found
972 pos -= 4; // skip the "tex(" part
973 if (expr[pos] == '*')
974 break; // two '*' in a row are definitely bad
975 expr.insert(pos, "*");
978 std::vector<string> tmp = getVectorFromString(out, "$$");
982 out = subst(tmp[1],"\\>", "");
983 lyxerr << "out: '" << out << "'" << endl;
985 // Ugly code that tries to make the result prettier
987 string::size_type i = out.find("\\mathchoice");
988 while (i != string::npos) {
989 string::size_type j = get_matching_brace(out, i + 12);
990 string::size_type k = get_matching_brace(out, j + 1);
991 k = get_matching_brace(out, k + 1);
992 k = get_matching_brace(out, k + 1);
993 string mid = out.substr(i + 13,j - i - 13);
994 if (mid.find("\\over") != string::npos)
995 mid = '{' + mid + '}';
996 out = out.substr(0,i)
999 //lyxerr << "out: " << out << endl;
1000 i = out.find("\\mathchoice", i);
1004 i = out.find("\\over");
1005 while (i != string::npos) {
1006 string::size_type j = get_matching_brace_back(out, i - 1);
1007 if (j == string::npos || j == 0) break;
1008 string::size_type k = get_matching_brace(out, i + 5);
1009 if (k == string::npos || k + 1 == out.size()) break;
1010 out = out.substr(0,j - 1)
1012 + out.substr(j,i - j)
1013 + out.substr(i + 5,k - i - 4)
1014 + out.substr(k + 2);
1015 //lyxerr << "out: " << out << endl;
1016 i = out.find("\\over", i + 4);
1019 mathed_parse_cell(res, out);
1024 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1026 string header = "readlib(latex):\n";
1028 // remove the \\it for variable names
1029 //"#`latex/csname_font` := `\\it `:"
1031 "`latex/csname_font` := ``:\n";
1033 // export matrices in (...) instead of [...]
1035 "`latex/latex/matrix` := "
1036 "subs(`[`=`(`, `]`=`)`,"
1037 "eval(`latex/latex/matrix`)):\n";
1039 // replace \\cdots with proper '*'
1041 "`latex/latex/*` := "
1042 "subs(`\\,`=`\\cdot `,"
1043 "eval(`latex/latex/*`)):\n";
1045 // remove spurious \\noalign{\\medskip} in matrix output
1047 "`latex/latex/matrix`:= "
1048 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1049 "eval(`latex/latex/matrix`)):\n";
1051 //"#`latex/latex/symbol` "
1052 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1054 string trailer = "quit;";
1058 string expr = STRCONV(os.str());
1059 lyxerr << "ar: '" << ar << "'\n"
1060 << "ms: '" << os.str() << "'" << endl;
1062 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1063 // try to fix missing '*' the hard way by using mint
1065 // ... > echo "1A;" | mint -i 1 -S -s -q
1068 // Probably missing an operator such as * p
1070 lyxerr << "checking expr: '" << expr << "'" << endl;
1071 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1073 break; // expression syntax is ok
1074 istringstream is(out.c_str());
1077 if (line.find("on line") != 0)
1078 break; // error message not identified
1080 string::size_type pos = line.find('^');
1081 if (pos == string::npos || pos < 15)
1082 break; // caret position not found
1083 pos -= 15; // skip the "on line ..." part
1084 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1085 break; // two '*' in a row are definitely bad
1086 expr.insert(pos, 1, '*');
1089 string full = "latex(" + extra + '(' + expr + "));";
1090 string out = captureOutput("maple -q", header + full + trailer);
1096 mathed_parse_cell(res, out);
1101 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1104 OctaveStream vs(os);
1106 string expr = STRCONV(os.str());
1109 lyxerr << "pipe: ar: '" << ar << "'\n"
1110 << "pipe: expr: '" << expr << "'" << endl;
1112 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1114 // try to fix missing '*' the hard way
1116 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1119 lyxerr << "checking expr: '" << expr << "'" << endl;
1120 out = captureOutput("octave -q 2>&1", expr);
1121 lyxerr << "checking out: '" << out << "'" << endl;
1123 // leave loop if expression syntax is probably ok
1124 if (out.find("parse error:") == string::npos)
1127 // search line with single caret
1128 istringstream is(out.c_str());
1132 lyxerr << "skipping line: '" << line << "'" << endl;
1133 if (line.find(">>> ") != string::npos)
1137 // found line with error, next line is the one with caret
1139 string::size_type pos = line.find('^');
1140 lyxerr << "caret line: '" << line << "'" << endl;
1141 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1142 if (pos == string::npos || pos < 4)
1143 break; // caret position not found
1144 pos -= 4; // skip the ">>> " part
1145 if (expr[pos] == '*')
1146 break; // two '*' in a row are definitely bad
1147 expr.insert(pos, 1, '*');
1154 out = out.substr(6);
1156 // parse output as matrix or single number
1157 MathAtom at(new MathArrayInset("array", out));
1158 MathArrayInset const * mat = at->asArrayInset();
1160 if (mat->ncols() == 1 && mat->nrows() == 1)
1161 res.append(mat->cell(0));
1163 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1164 res.back().nucleus()->cell(0).push_back(at);
1172 MathArray pipeThroughExtern(string const & lang, string const & extra,
1173 MathArray const & ar)
1175 if (lang == "octave")
1176 return pipeThroughOctave(extra, ar);
1178 if (lang == "maxima")
1179 return pipeThroughMaxima(extra, ar);
1181 if (lang == "maple")
1182 return pipeThroughMaple(extra, ar);
1184 // create normalized expression
1186 NormalStream ns(os);
1187 os << '[' << extra << ' ';
1190 string data = STRCONV(os.str());
1192 // search external script
1193 string file = LibFileSearch("mathed", "extern_" + lang);
1195 lyxerr << "converter to '" << lang << "' not found" << endl;
1199 // run external sript
1200 string out = captureOutput(file, data);
1202 mathed_parse_cell(res, out);