6 // This file contains most of the magic that extracts "context
7 // information" from the unstructered layout-oriented stuff in an
12 #include "math_amsarrayinset.h"
13 #include "math_arrayinset.h"
14 #include "math_charinset.h"
15 #include "math_deliminset.h"
16 #include "math_diffinset.h"
17 #include "math_exfuncinset.h"
18 #include "math_exintinset.h"
19 #include "math_fracinset.h"
20 #include "math_liminset.h"
21 #include "math_matrixinset.h"
22 #include "math_mathmlstream.h"
23 #include "math_numberinset.h"
24 #include "math_scriptinset.h"
25 #include "math_stringinset.h"
26 #include "math_symbolinset.h"
27 #include "math_unknowninset.h"
28 #include "math_parser.h"
31 #include "support/lyxlib.h"
32 #include "support/systemcall.h"
33 #include "support/filetools.h"
34 #include "support/lstrings.h"
39 using std::istringstream;
44 ostream & operator<<(ostream & os, MathArray const & ar)
52 // define a function for tests
53 typedef bool TestItemFunc(MathAtom const &);
55 // define a function for replacing subexpressions
56 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
60 // try to extract a super/subscript
61 // modify iterator position to point behind the thing
62 bool extractScript(MathArray & ar,
63 MathArray::iterator & pos, MathArray::iterator last)
65 // nothing to get here
69 // is this a scriptinset?
70 if (!(*pos)->asScriptInset())
73 // it is a scriptinset, use it.
80 // try to extract an "argument" to some function.
81 // returns position behind the argument
82 MathArray::iterator extractArgument(MathArray & ar,
83 MathArray::iterator pos, MathArray::iterator last, string const & = "")
85 // nothing to get here
89 // something deliminited _is_ an argument
90 if ((*pos)->asDelimInset()) {
95 // always take the first thing, no matter what it is
98 // go ahead if possible
103 // if the next item is a subscript, it most certainly belongs to the
105 extractScript(ar, pos, last);
109 // but it might be more than that.
110 // FIXME: not implemented
111 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
112 // // always take the first thing, no matter
114 // ar.push_back(*it);
122 // returns sequence of char with same code starting at it up to end
123 // it might be less, though...
125 (MathArray::const_iterator it, MathArray::const_iterator end)
128 for (; it != end && (*it)->asCharInset(); ++it)
129 s += (*it)->getChar();
134 void extractStrings(MathArray & ar)
136 //lyxerr << "\nStrings from: " << ar << "\n";
137 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
138 if (!ar[i]->asCharInset())
140 string s = charSequence(ar.begin() + i, ar.end());
141 ar[i] = MathAtom(new MathStringInset(s));
142 ar.erase(i + 1, i + s.size());
144 //lyxerr << "\nStrings to: " << ar << "\n";
148 void extractMatrices(MathArray & ar)
150 //lyxerr << "\nMatrices from: " << ar << "\n";
151 // first pass for explicitly delimited stuff
152 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
153 if (!ar[i]->asDelimInset())
155 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
158 if (!arr.front()->asGridInset())
160 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
163 // second pass for AMS "pmatrix" etc
164 for (MathArray::size_type i = 0; i < ar.size(); ++i)
165 if (ar[i]->asAMSArrayInset())
166 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
167 //lyxerr << "\nMatrices to: " << ar << "\n";
171 // convert this inset somehow to a string
172 bool extractString(MathAtom const & at, string & str)
175 str = string(1, at->getChar());
178 if (at->asStringInset()) {
179 str = at->asStringInset()->str();
186 // convert this inset somehow to a number
187 bool extractNumber(MathArray const & ar, int & i)
189 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
195 bool extractNumber(MathArray const & ar, double & d)
197 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
203 bool testString(MathAtom const & at, const string & str)
206 return extractString(at, s) && str == s;
210 // search end of nested sequence
211 MathArray::iterator endNestSearch(
212 MathArray::iterator it,
213 MathArray::iterator last,
214 TestItemFunc testOpen,
215 TestItemFunc testClose
218 for (int level = 0; it != last; ++it) {
230 // replace nested sequences by a real Insets
233 TestItemFunc testOpen,
234 TestItemFunc testClose,
235 ReplaceArgumentFunc replaceArg
238 // use indices rather than iterators for the loop because we are going
239 // to modify the array.
240 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
241 // check whether this is the begin of the sequence
242 if (!testOpen(ar[i]))
245 // search end of sequence
246 MathArray::iterator it = ar.begin() + i;
247 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
251 // replace the original stuff by the new inset
252 ar[i] = replaceArg(MathArray(it + 1, jt));
253 ar.erase(it + 1, jt + 1);
260 // split scripts into seperate super- and subscript insets. sub goes in
264 void splitScripts(MathArray & ar)
266 //lyxerr << "\nScripts from: " << ar << "\n";
267 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
268 // is this script inset?
269 if (!ar[i]->asScriptInset())
272 // no problem if we don't have both...
273 if (!ar[i]->asScriptInset()->hasUp())
275 if (!ar[i]->asScriptInset()->hasDown())
278 // create extra script inset and move superscript over
279 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
280 MathScriptInset * q = new MathScriptInset(true);
281 std::swap(q->up(), p->up());
282 p->removeScript(true);
284 // insert new inset behind
286 ar.insert(i, MathAtom(q));
288 //lyxerr << "\nScripts to: " << ar << "\n";
296 void extractExps(MathArray & ar)
298 //lyxerr << "\nExps from: " << ar << "\n";
299 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
301 if (ar[i]->getChar() != 'e')
304 // we need an exponent but no subscript
305 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
306 if (!sup || sup->hasDown())
309 // create a proper exp-inset as replacement
310 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
313 //lyxerr << "\nExps to: " << ar << "\n";
318 // extract det(...) from |matrix|
320 void extractDets(MathArray & ar)
322 //lyxerr << "\ndet from: " << ar << "\n";
323 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
324 MathDelimInset const * del = (*it)->asDelimInset();
329 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
331 //lyxerr << "\ndet to: " << ar << "\n";
339 bool isDigitOrSimilar(char c)
341 return ('0' <= c && c <= '9') || c == '.';
345 // returns sequence of digits
347 (MathArray::const_iterator it, MathArray::const_iterator end)
350 for (; it != end && (*it)->asCharInset(); ++it) {
351 if (!isDigitOrSimilar((*it)->getChar()))
353 s += (*it)->getChar();
359 void extractNumbers(MathArray & ar)
361 //lyxerr << "\nNumbers from: " << ar << "\n";
362 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
363 if (!ar[i]->asCharInset())
365 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
368 string s = digitSequence(ar.begin() + i, ar.end());
370 ar[i] = MathAtom(new MathNumberInset(s));
371 ar.erase(i + 1, i + s.size());
373 //lyxerr << "\nNumbers to: " << ar << "\n";
379 // search deliminiters
382 bool testOpenParan(MathAtom const & at)
384 return testString(at, "(");
388 bool testCloseParan(MathAtom const & at)
390 return testString(at, ")");
394 MathAtom replaceDelims(const MathArray & ar)
396 return MathAtom(new MathDelimInset("(", ")", ar));
400 // replace '('...')' sequences by a real MathDelimInset
401 void extractDelims(MathArray & ar)
403 //lyxerr << "\nDelims from: " << ar << "\n";
404 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
405 //lyxerr << "\nDelims to: " << ar << "\n";
411 // search well-known functions
415 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
416 // assume 'extractDelims' ran before
417 void extractFunctions(MathArray & ar)
419 // we need at least two items...
423 //lyxerr << "\nFunctions from: " << ar << "\n";
424 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
425 MathArray::iterator it = ar.begin() + i;
426 MathArray::iterator jt = it + 1;
430 if ((*it)->asUnknownInset()) {
431 // it certainly is if it is well known...
432 name = (*it)->name();
434 // is this a user defined function?
435 // it it probably not, if it doesn't have a name.
436 if (!extractString(*it, name))
438 // it is not if it has no argument
441 // guess so, if this is followed by
442 // a DelimInset with a single item in the cell
443 MathDelimInset const * del = (*jt)->asDelimInset();
444 if (!del || del->cell(0).size() != 1)
446 // fall trough into main branch
449 // do we have an exponent like in
450 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
452 extractScript(exp, jt, ar.end());
454 // create a proper inset as replacement
455 MathExFuncInset * p = new MathExFuncInset(name);
457 // jt points to the "argument". Get hold of this.
458 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
460 // replace the function name by a real function inset
463 // remove the source of the argument from the array
464 ar.erase(it + 1, st);
466 // re-insert exponent
467 ar.insert(i + 1, exp);
468 //lyxerr << "\nFunctions to: " << ar << "\n";
477 bool testSymbol(MathAtom const & at, string const & name)
479 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
483 bool testIntSymbol(MathAtom const & at)
485 return testSymbol(at, "int");
489 bool testIntegral(MathAtom const & at)
493 ( at->asScriptInset()
494 && at->asScriptInset()->nuc().size()
495 && testIntSymbol(at->asScriptInset()->nuc().back()) );
500 bool testIntDiff(MathAtom const & at)
502 return testString(at, "d");
506 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
507 // assume 'extractDelims' ran before
508 void extractIntegrals(MathArray & ar)
510 // we need at least three items...
514 //lyxerr << "\nIntegrals from: " << ar << "\n";
515 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
516 MathArray::iterator it = ar.begin() + i;
519 MathArray::iterator jt =
520 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
522 // something sensible found?
526 // is this a integral name?
527 if (!testIntegral(*it))
530 // core ist part from behind the scripts to the 'd'
531 MathExIntInset * p = new MathExIntInset("int");
533 // handle scripts if available
534 if (!testIntSymbol(*it)) {
535 p->cell(2) = (*it)->asScriptInset()->down();
536 p->cell(3) = (*it)->asScriptInset()->up();
538 p->cell(0) = MathArray(it + 1, jt);
540 // use the "thing" behind the 'd' as differential
541 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
544 ar.erase(it + 1, tt);
547 //lyxerr << "\nIntegrals to: " << ar << "\n";
556 bool testEqualSign(MathAtom const & at)
558 return testString(at, "=");
562 bool testSumSymbol(MathAtom const & p)
564 return testSymbol(p, "sum");
568 bool testSum(MathAtom const & at)
572 ( at->asScriptInset()
573 && at->asScriptInset()->nuc().size()
574 && testSumSymbol(at->asScriptInset()->nuc().back()) );
578 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
579 // assume 'extractDelims' ran before
580 void extractSums(MathArray & ar)
582 // we need at least two items...
586 //lyxerr << "\nSums from: " << ar << "\n";
587 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
588 MathArray::iterator it = ar.begin() + i;
590 // is this a sum name?
594 // create a proper inset as replacement
595 MathExIntInset * p = new MathExIntInset("sum");
597 // collect lower bound and summation index
598 MathScriptInset const * sub = ar[i]->asScriptInset();
599 if (sub && sub->hasDown()) {
600 // try to figure out the summation index from the subscript
601 MathArray const & ar = sub->down();
602 MathArray::const_iterator xt =
603 find_if(ar.begin(), ar.end(), &testEqualSign);
604 if (xt != ar.end()) {
605 // we found a '=', use everything in front of that as index,
606 // and everything behind as lower index
607 p->cell(1) = MathArray(ar.begin(), xt);
608 p->cell(2) = MathArray(xt + 1, ar.end());
610 // use everything as summation index, don't use scripts.
615 // collect upper bound
616 if (sub && sub->hasUp())
617 p->cell(3) = sub->up();
619 // use something behind the script as core
620 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
623 ar.erase(it + 1, tt);
626 //lyxerr << "\nSums to: " << ar << "\n";
631 // search differential stuff
634 // tests for 'd' or '\partial'
635 bool testDiffItem(MathAtom const & at)
637 return testString(at, "d");
641 bool testDiffArray(MathArray const & ar)
643 return ar.size() && testDiffItem(ar.front());
647 bool testDiffFrac(MathAtom const & at)
651 && testDiffArray(at->asFracInset()->cell(0))
652 && testDiffArray(at->asFracInset()->cell(1));
656 void extractDiff(MathArray & ar)
658 //lyxerr << "\nDiffs from: " << ar << "\n";
659 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
660 MathArray::iterator it = ar.begin() + i;
662 // is this a "differential fraction"?
663 if (!testDiffFrac(*it))
666 MathFracInset const * f = (*it)->asFracInset();
668 lyxerr << "should not happen\n";
672 // create a proper diff inset
673 MathDiffInset * diff = new MathDiffInset;
675 // collect function, let jt point behind last used item
676 MathArray::iterator jt = it + 1;
678 MathArray const & numer = f->cell(0);
679 if (numer.size() > 1 && numer[1]->asScriptInset()) {
680 // this is something like d^n f(x) / d... or d^n / d...
683 if (numer.size() > 2)
684 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
686 jt = extractArgument(diff->cell(0), jt, ar.end());
688 // simply d f(x) / d... or d/d...
689 if (numer.size() > 1)
690 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
692 jt = extractArgument(diff->cell(0), jt, ar.end());
695 // collect denominator parts
696 MathArray const & denom = f->cell(1);
697 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
699 MathArray::const_iterator et
700 = find_if(dt + 1, denom.end(), &testDiffItem);
703 MathArray::const_iterator st = et - 1;
704 MathScriptInset const * script = (*st)->asScriptInset();
705 if (script && script->hasUp()) {
706 // things like d.../dx^n
708 if (extractNumber(script->up(), mult)) {
709 //lyxerr << "mult: " << mult << endl;
710 for (int i = 0; i < mult; ++i)
711 diff->addDer(MathArray(dt + 1, st));
715 diff->addDer(MathArray(dt + 1, et));
721 ar.erase(it + 1, jt);
722 *it = MathAtom(diff);
724 //lyxerr << "\nDiffs to: " << ar << "\n";
733 bool testRightArrow(MathAtom const & at)
735 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
740 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
741 // assume 'extractDelims' ran before
742 void extractLims(MathArray & ar)
744 // we need at least three items...
748 //lyxerr << "\nLimits from: " << ar << "\n";
749 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
750 MathArray::iterator it = ar.begin() + i;
752 // is this a limit function?
753 if (!testSymbol(*it, "lim"))
756 // the next one must be a subscript (without superscript)
757 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
758 if (!sub || !sub->hasDown() || sub->hasUp())
761 // and it must contain a -> symbol
762 MathArray const & s = sub->down();
763 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
767 // the -> splits the subscript int x and x0
768 MathArray x = MathArray(s.begin(), st);
769 MathArray x0 = MathArray(st + 1, s.end());
771 // use something behind the script as core
773 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
776 ar.erase(it + 1, tt);
778 // create a proper inset as replacement
779 *it = MathAtom(new MathLimInset(f, x, x0));
781 //lyxerr << "\nLimits to: " << ar << "\n";
789 void extractStructure(MathArray & ar)
791 //lyxerr << "\nStructure from: " << ar << "\n";
792 extractIntegrals(ar);
798 extractFunctions(ar);
804 //lyxerr << "\nStructure to: " << ar << "\n";
808 void write(MathArray const & dat, WriteStream & wi)
812 wi.firstitem() = true;
813 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
815 wi.firstitem() = false;
820 void normalize(MathArray const & ar, NormalStream & os)
822 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
823 (*it)->normalize(os);
827 void octavize(MathArray const & dat, OctaveStream & os)
830 extractStructure(ar);
831 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
836 void maplize(MathArray const & dat, MapleStream & os)
839 extractStructure(ar);
840 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
845 void maximize(MathArray const & dat, MaximaStream & os)
848 extractStructure(ar);
849 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
854 void mathematicize(MathArray const & dat, MathematicaStream & os)
857 extractStructure(ar);
858 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
859 (*it)->mathematicize(os);
863 void mathmlize(MathArray const & dat, MathMLStream & os)
866 extractStructure(ar);
869 else if (ar.size() == 1)
873 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
874 (*it)->mathmlize(os);
884 string captureOutput(string const & cmd, string const & data)
886 string command = "echo '" + data + "' | " + cmd;
887 lyxerr << "calling: " << command << endl;
888 cmd_ret const ret = RunCommand(command);
892 string::size_type get_matching_brace(string const & str, string::size_type i)
895 string::size_type n = str.size();
897 i = str.find_first_of("{}", i+1);
898 if (i == string::npos) return i;
909 string::size_type get_matching_brace_back(string const & str, string::size_type i)
913 i = str.find_last_of("{}", i-1);
914 if (i == string::npos) return i;
925 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
930 string expr = os.str().c_str();
931 string const header = "SIMPSUM:true;";
934 for (int i = 0; i < 100; ++i) { // at most 100 attempts
935 // try to fix missing '*' the hard way
937 // > echo "2x;" | maxima
939 // (C1) Incorrect syntax: x is not an infix operator
943 lyxerr << "checking expr: '" << expr << "'\n";
944 string full = header + "tex(" + expr + ");";
945 out = captureOutput("maxima", full);
947 // leave loop if expression syntax is probably ok
948 if (out.find("Incorrect syntax") == string::npos)
951 // search line with "Incorrect syntax"
952 istringstream is(out.c_str());
956 if (line.find("Incorrect syntax") != string::npos)
960 // 2nd next line is the one with caret
963 string::size_type pos = line.find('^');
964 lyxerr << "found caret at pos: '" << pos << "'\n";
965 if (pos == string::npos || pos < 4)
966 break; // caret position not found
967 pos -= 4; // skip the "tex(" part
968 if (expr[pos] == '*')
969 break; // two '*' in a row are definitely bad
970 expr.insert(pos, "*");
973 std::vector<string> tmp = getVectorFromString(out, "$$");
977 out = subst(tmp[1],"\\>", "");
978 lyxerr << "out: '" << out << "'\n";
980 // Ugly code that tries to make the result prettier
982 string::size_type i = out.find("\\mathchoice");
983 while (i != string::npos) {
984 string::size_type j = get_matching_brace(out, i + 12);
985 string::size_type k = get_matching_brace(out, j + 1);
986 k = get_matching_brace(out, k + 1);
987 k = get_matching_brace(out, k + 1);
988 string mid = out.substr(i + 13,j - i - 13);
989 if (mid.find("\\over") != string::npos)
990 mid = '{' + mid + '}';
991 out = out.substr(0,i)
994 //lyxerr << "out: " << out << endl;
995 i = out.find("\\mathchoice", i);
999 i = out.find("\\over");
1000 while (i != string::npos) {
1001 string::size_type j = get_matching_brace_back(out, i - 1);
1002 if (j == string::npos || j == 0) break;
1003 string::size_type k = get_matching_brace(out, i + 5);
1004 if (k == string::npos || k + 1 == out.size()) break;
1005 out = out.substr(0,j - 1)
1007 + out.substr(j,i - j)
1008 + out.substr(i + 5,k - i - 4)
1009 + out.substr(k + 2);
1010 //lyxerr << "out: " << out << endl;
1011 i = out.find("\\over", i + 4);
1014 mathed_parse_cell(res, out);
1019 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1021 string header = "readlib(latex):\n";
1023 // remove the \\it for variable names
1024 //"#`latex/csname_font` := `\\it `:"
1026 "`latex/csname_font` := ``:\n";
1028 // export matrices in (...) instead of [...]
1030 "`latex/latex/matrix` := "
1031 "subs(`[`=`(`, `]`=`)`,"
1032 "eval(`latex/latex/matrix`)):\n";
1034 // replace \\cdots with proper '*'
1036 "`latex/latex/*` := "
1037 "subs(`\\,`=`\\cdot `,"
1038 "eval(`latex/latex/*`)):\n";
1040 // remove spurious \\noalign{\\medskip} in matrix output
1042 "`latex/latex/matrix`:= "
1043 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1044 "eval(`latex/latex/matrix`)):\n";
1046 //"#`latex/latex/symbol` "
1047 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1049 string trailer = "quit;";
1053 string expr = os.str().c_str();
1054 lyxerr << "ar: '" << ar << "'\n";
1055 lyxerr << "ms: '" << os.str() << "'\n";
1057 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1058 // try to fix missing '*' the hard way by using mint
1060 // ... > echo "1A;" | mint -i 1 -S -s -q
1063 // Probably missing an operator such as * p
1065 lyxerr << "checking expr: '" << expr << "'\n";
1066 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ";");
1068 break; // expression syntax is ok
1069 istringstream is(out.c_str());
1072 if (line.find("on line") != 0)
1073 break; // error message not identified
1075 string::size_type pos = line.find('^');
1076 if (pos == string::npos || pos < 15)
1077 break; // caret position not found
1078 pos -= 15; // skip the "on line ..." part
1079 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1080 break; // two '*' in a row are definitely bad
1081 expr.insert(pos, "*");
1084 string full = "latex(" + extra + '(' + expr + "));";
1085 string out = captureOutput("maple -q", header + full + trailer);
1091 mathed_parse_cell(res, out);
1096 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1099 OctaveStream vs(os);
1101 string expr = os.str().c_str();
1104 lyxerr << "pipe: ar: '" << ar << "'\n";
1105 lyxerr << "pipe: expr: '" << expr << "'\n";
1107 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1109 // try to fix missing '*' the hard way
1111 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1114 lyxerr << "checking expr: '" << expr << "'\n";
1115 out = captureOutput("octave -q 2>&1", expr);
1116 lyxerr << "checking out: '" << out << "'\n";
1118 // leave loop if expression syntax is probably ok
1119 if (out.find("parse error:") == string::npos)
1122 // search line with single caret
1123 istringstream is(out.c_str());
1127 lyxerr << "skipping line: '" << line << "'\n";
1128 if (line.find(">>> ") != string::npos)
1132 // found line with error, next line is the one with caret
1134 string::size_type pos = line.find('^');
1135 lyxerr << "caret line: '" << line << "'\n";
1136 lyxerr << "found caret at pos: '" << pos << "'\n";
1137 if (pos == string::npos || pos < 4)
1138 break; // caret position not found
1139 pos -= 4; // skip the ">>> " part
1140 if (expr[pos] == '*')
1141 break; // two '*' in a row are definitely bad
1142 expr.insert(pos, "*");
1149 out = out.substr(6);
1151 // parse output as matrix or single number
1152 MathAtom at(new MathArrayInset("array", out));
1153 MathArrayInset const * mat = at->asArrayInset();
1155 if (mat->ncols() == 1 && mat->nrows() == 1)
1156 res.append(mat->cell(0));
1158 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1159 res.back().nucleus()->cell(0).push_back(at);
1167 MathArray pipeThroughExtern(string const & lang, string const & extra,
1168 MathArray const & ar)
1170 if (lang == "octave")
1171 return pipeThroughOctave(extra, ar);
1173 if (lang == "maxima")
1174 return pipeThroughMaxima(extra, ar);
1176 if (lang == "maple")
1177 return pipeThroughMaple(extra, ar);
1179 // create normalized expression
1181 NormalStream ns(os);
1182 os << "[" << extra << ' ';
1185 string data = os.str().c_str();
1187 // search external script
1188 string file = LibFileSearch("mathed", "extern_" + lang);
1190 lyxerr << "converter to '" << lang << "' not found\n";
1194 // run external sript
1195 string out = captureOutput(file, data);
1197 mathed_parse_cell(res, out);