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"
29 #include "support/lstrings.h"
33 using namespace lyx::support;
36 using std::istringstream;
41 ostream & operator<<(ostream & os, MathArray const & ar)
49 // define a function for tests
50 typedef bool TestItemFunc(MathAtom const &);
52 // define a function for replacing subexpressions
53 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
57 // try to extract a super/subscript
58 // modify iterator position to point behind the thing
59 bool extractScript(MathArray & ar,
60 MathArray::iterator & pos, MathArray::iterator last)
62 // nothing to get here
66 // is this a scriptinset?
67 if (!(*pos)->asScriptInset())
70 // it is a scriptinset, use it.
77 // try to extract an "argument" to some function.
78 // returns position behind the argument
79 MathArray::iterator extractArgument(MathArray & ar,
80 MathArray::iterator pos, MathArray::iterator last, string const & = "")
82 // nothing to get here
86 // something deliminited _is_ an argument
87 if ((*pos)->asDelimInset()) {
92 // always take the first thing, no matter what it is
95 // go ahead if possible
100 // if the next item is a subscript, it most certainly belongs to the
102 extractScript(ar, pos, last);
106 // but it might be more than that.
107 // FIXME: not implemented
108 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
109 // // always take the first thing, no matter
111 // ar.push_back(*it);
119 // returns sequence of char with same code starting at it up to end
120 // it might be less, though...
122 (MathArray::const_iterator it, MathArray::const_iterator end)
125 for (; it != end && (*it)->asCharInset(); ++it)
126 s += (*it)->getChar();
131 void extractStrings(MathArray & ar)
133 //lyxerr << "\nStrings from: " << ar << "\n";
134 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
135 if (!ar[i]->asCharInset())
137 string s = charSequence(ar.begin() + i, ar.end());
138 ar[i] = MathAtom(new MathStringInset(s));
139 ar.erase(i + 1, i + s.size());
141 //lyxerr << "\nStrings to: " << ar << "\n";
145 void extractMatrices(MathArray & ar)
147 //lyxerr << "\nMatrices from: " << ar << "\n";
148 // first pass for explicitly delimited stuff
149 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
150 if (!ar[i]->asDelimInset())
152 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
155 if (!arr.front()->asGridInset())
157 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
160 // second pass for AMS "pmatrix" etc
161 for (MathArray::size_type i = 0; i < ar.size(); ++i)
162 if (ar[i]->asAMSArrayInset())
163 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
164 //lyxerr << "\nMatrices to: " << ar << "\n";
168 // convert this inset somehow to a string
169 bool extractString(MathAtom const & at, string & str)
172 str = string(1, at->getChar());
175 if (at->asStringInset()) {
176 str = at->asStringInset()->str();
183 // convert this inset somehow to a number
184 bool extractNumber(MathArray const & ar, int & i)
186 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
192 bool extractNumber(MathArray const & ar, double & d)
194 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
200 bool testString(MathAtom const & at, string const & str)
203 return extractString(at, s) && str == s;
207 // search end of nested sequence
208 MathArray::iterator endNestSearch(
209 MathArray::iterator it,
210 MathArray::iterator last,
211 TestItemFunc testOpen,
212 TestItemFunc testClose
215 for (int level = 0; it != last; ++it) {
227 // replace nested sequences by a real Insets
230 TestItemFunc testOpen,
231 TestItemFunc testClose,
232 ReplaceArgumentFunc replaceArg
235 // use indices rather than iterators for the loop because we are going
236 // to modify the array.
237 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
238 // check whether this is the begin of the sequence
239 if (!testOpen(ar[i]))
242 // search end of sequence
243 MathArray::iterator it = ar.begin() + i;
244 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
248 // replace the original stuff by the new inset
249 ar[i] = replaceArg(MathArray(it + 1, jt));
250 ar.erase(it + 1, jt + 1);
257 // split scripts into seperate super- and subscript insets. sub goes in
261 void splitScripts(MathArray & ar)
263 //lyxerr << "\nScripts from: " << ar << "\n";
264 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
265 // is this script inset?
266 if (!ar[i]->asScriptInset())
269 // no problem if we don't have both...
270 if (!ar[i]->asScriptInset()->hasUp())
272 if (!ar[i]->asScriptInset()->hasDown())
275 // create extra script inset and move superscript over
276 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
277 MathScriptInset * q = new MathScriptInset(true);
278 std::swap(q->up(), p->up());
279 p->removeScript(true);
281 // insert new inset behind
283 ar.insert(i, MathAtom(q));
285 //lyxerr << "\nScripts to: " << ar << "\n";
293 void extractExps(MathArray & ar)
295 //lyxerr << "\nExps from: " << ar << "\n";
296 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
298 if (ar[i]->getChar() != 'e')
301 // we need an exponent but no subscript
302 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
303 if (!sup || sup->hasDown())
306 // create a proper exp-inset as replacement
307 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
310 //lyxerr << "\nExps to: " << ar << "\n";
315 // extract det(...) from |matrix|
317 void extractDets(MathArray & ar)
319 //lyxerr << "\ndet from: " << ar << "\n";
320 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
321 MathDelimInset const * del = (*it)->asDelimInset();
326 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
328 //lyxerr << "\ndet to: " << ar << "\n";
336 bool isDigitOrSimilar(char c)
338 return ('0' <= c && c <= '9') || c == '.';
342 // returns sequence of digits
344 (MathArray::const_iterator it, MathArray::const_iterator end)
347 for (; it != end && (*it)->asCharInset(); ++it) {
348 if (!isDigitOrSimilar((*it)->getChar()))
350 s += (*it)->getChar();
356 void extractNumbers(MathArray & ar)
358 //lyxerr << "\nNumbers from: " << ar << "\n";
359 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
360 if (!ar[i]->asCharInset())
362 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
365 string s = digitSequence(ar.begin() + i, ar.end());
367 ar[i] = MathAtom(new MathNumberInset(s));
368 ar.erase(i + 1, i + s.size());
370 //lyxerr << "\nNumbers to: " << ar << "\n";
376 // search deliminiters
379 bool testOpenParan(MathAtom const & at)
381 return testString(at, "(");
385 bool testCloseParan(MathAtom const & at)
387 return testString(at, ")");
391 MathAtom replaceDelims(const MathArray & ar)
393 return MathAtom(new MathDelimInset("(", ")", ar));
397 // replace '('...')' sequences by a real MathDelimInset
398 void extractDelims(MathArray & ar)
400 //lyxerr << "\nDelims from: " << ar << "\n";
401 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
402 //lyxerr << "\nDelims to: " << ar << "\n";
408 // search well-known functions
412 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
413 // assume 'extractDelims' ran before
414 void extractFunctions(MathArray & ar)
416 // we need at least two items...
420 //lyxerr << "\nFunctions from: " << ar << "\n";
421 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
422 MathArray::iterator it = ar.begin() + i;
423 MathArray::iterator jt = it + 1;
427 if ((*it)->asUnknownInset()) {
428 // it certainly is if it is well known...
429 name = (*it)->name();
431 // is this a user defined function?
432 // it it probably not, if it doesn't have a name.
433 if (!extractString(*it, name))
435 // it is not if it has no argument
438 // guess so, if this is followed by
439 // a DelimInset with a single item in the cell
440 MathDelimInset const * del = (*jt)->asDelimInset();
441 if (!del || del->cell(0).size() != 1)
443 // fall trough into main branch
446 // do we have an exponent like in
447 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
449 extractScript(exp, jt, ar.end());
451 // create a proper inset as replacement
452 MathExFuncInset * p = new MathExFuncInset(name);
454 // jt points to the "argument". Get hold of this.
455 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
457 // replace the function name by a real function inset
460 // remove the source of the argument from the array
461 ar.erase(it + 1, st);
463 // re-insert exponent
464 ar.insert(i + 1, exp);
465 //lyxerr << "\nFunctions to: " << ar << "\n";
474 bool testSymbol(MathAtom const & at, string const & name)
476 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
480 bool testIntSymbol(MathAtom const & at)
482 return testSymbol(at, "int");
486 bool testIntegral(MathAtom const & at)
490 ( at->asScriptInset()
491 && at->asScriptInset()->nuc().size()
492 && testIntSymbol(at->asScriptInset()->nuc().back()) );
497 bool testIntDiff(MathAtom const & at)
499 return testString(at, "d");
503 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
504 // assume 'extractDelims' ran before
505 void extractIntegrals(MathArray & ar)
507 // we need at least three items...
511 //lyxerr << "\nIntegrals from: " << ar << "\n";
512 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
513 MathArray::iterator it = ar.begin() + i;
516 MathArray::iterator jt =
517 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
519 // something sensible found?
523 // is this a integral name?
524 if (!testIntegral(*it))
527 // core ist part from behind the scripts to the 'd'
528 MathExIntInset * p = new MathExIntInset("int");
530 // handle scripts if available
531 if (!testIntSymbol(*it)) {
532 p->cell(2) = (*it)->asScriptInset()->down();
533 p->cell(3) = (*it)->asScriptInset()->up();
535 p->cell(0) = MathArray(it + 1, jt);
537 // use the "thing" behind the 'd' as differential
538 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
541 ar.erase(it + 1, tt);
544 //lyxerr << "\nIntegrals to: " << ar << "\n";
553 bool testEqualSign(MathAtom const & at)
555 return testString(at, "=");
559 bool testSumSymbol(MathAtom const & p)
561 return testSymbol(p, "sum");
565 bool testSum(MathAtom const & at)
569 ( at->asScriptInset()
570 && at->asScriptInset()->nuc().size()
571 && testSumSymbol(at->asScriptInset()->nuc().back()) );
575 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
576 // assume 'extractDelims' ran before
577 void extractSums(MathArray & ar)
579 // we need at least two items...
583 //lyxerr << "\nSums from: " << ar << "\n";
584 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
585 MathArray::iterator it = ar.begin() + i;
587 // is this a sum name?
591 // create a proper inset as replacement
592 MathExIntInset * p = new MathExIntInset("sum");
594 // collect lower bound and summation index
595 MathScriptInset const * sub = ar[i]->asScriptInset();
596 if (sub && sub->hasDown()) {
597 // try to figure out the summation index from the subscript
598 MathArray const & ar = sub->down();
599 MathArray::const_iterator xt =
600 find_if(ar.begin(), ar.end(), &testEqualSign);
601 if (xt != ar.end()) {
602 // we found a '=', use everything in front of that as index,
603 // and everything behind as lower index
604 p->cell(1) = MathArray(ar.begin(), xt);
605 p->cell(2) = MathArray(xt + 1, ar.end());
607 // use everything as summation index, don't use scripts.
612 // collect upper bound
613 if (sub && sub->hasUp())
614 p->cell(3) = sub->up();
616 // use something behind the script as core
617 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
620 ar.erase(it + 1, tt);
623 //lyxerr << "\nSums to: " << ar << "\n";
628 // search differential stuff
631 // tests for 'd' or '\partial'
632 bool testDiffItem(MathAtom const & at)
634 return testString(at, "d");
638 bool testDiffArray(MathArray const & ar)
640 return ar.size() && testDiffItem(ar.front());
644 bool testDiffFrac(MathAtom const & at)
648 && testDiffArray(at->asFracInset()->cell(0))
649 && testDiffArray(at->asFracInset()->cell(1));
653 void extractDiff(MathArray & ar)
655 //lyxerr << "\nDiffs from: " << ar << "\n";
656 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
657 MathArray::iterator it = ar.begin() + i;
659 // is this a "differential fraction"?
660 if (!testDiffFrac(*it))
663 MathFracInset const * f = (*it)->asFracInset();
665 lyxerr << "should not happen\n";
669 // create a proper diff inset
670 MathDiffInset * diff = new MathDiffInset;
672 // collect function, let jt point behind last used item
673 MathArray::iterator jt = it + 1;
675 MathArray const & numer = f->cell(0);
676 if (numer.size() > 1 && numer[1]->asScriptInset()) {
677 // this is something like d^n f(x) / d... or d^n / d...
680 if (numer.size() > 2)
681 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
683 jt = extractArgument(diff->cell(0), jt, ar.end());
685 // simply d f(x) / d... or d/d...
686 if (numer.size() > 1)
687 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
689 jt = extractArgument(diff->cell(0), jt, ar.end());
692 // collect denominator parts
693 MathArray const & denom = f->cell(1);
694 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
696 MathArray::const_iterator et
697 = find_if(dt + 1, denom.end(), &testDiffItem);
700 MathArray::const_iterator st = et - 1;
701 MathScriptInset const * script = (*st)->asScriptInset();
702 if (script && script->hasUp()) {
703 // things like d.../dx^n
705 if (extractNumber(script->up(), mult)) {
706 //lyxerr << "mult: " << mult << endl;
707 for (int i = 0; i < mult; ++i)
708 diff->addDer(MathArray(dt + 1, st));
712 diff->addDer(MathArray(dt + 1, et));
718 ar.erase(it + 1, jt);
719 *it = MathAtom(diff);
721 //lyxerr << "\nDiffs to: " << ar << "\n";
730 bool testRightArrow(MathAtom const & at)
732 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
737 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
738 // assume 'extractDelims' ran before
739 void extractLims(MathArray & ar)
741 // we need at least three items...
745 //lyxerr << "\nLimits from: " << ar << "\n";
746 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
747 MathArray::iterator it = ar.begin() + i;
749 // is this a limit function?
750 if (!testSymbol(*it, "lim"))
753 // the next one must be a subscript (without superscript)
754 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
755 if (!sub || !sub->hasDown() || sub->hasUp())
758 // and it must contain a -> symbol
759 MathArray const & s = sub->down();
760 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
764 // the -> splits the subscript int x and x0
765 MathArray x = MathArray(s.begin(), st);
766 MathArray x0 = MathArray(st + 1, s.end());
768 // use something behind the script as core
770 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
773 ar.erase(it + 1, tt);
775 // create a proper inset as replacement
776 *it = MathAtom(new MathLimInset(f, x, x0));
778 //lyxerr << "\nLimits to: " << ar << "\n";
786 void extractStructure(MathArray & ar)
788 //lyxerr << "\nStructure from: " << ar << "\n";
789 extractIntegrals(ar);
795 extractFunctions(ar);
801 //lyxerr << "\nStructure to: " << ar << "\n";
805 void write(MathArray const & dat, WriteStream & wi)
809 wi.firstitem() = true;
810 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
812 wi.firstitem() = false;
817 void normalize(MathArray const & ar, NormalStream & os)
819 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
820 (*it)->normalize(os);
824 void octave(MathArray const & dat, OctaveStream & os)
827 extractStructure(ar);
828 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
833 void maple(MathArray const & dat, MapleStream & os)
836 extractStructure(ar);
837 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
842 void maxima(MathArray const & dat, MaximaStream & os)
845 extractStructure(ar);
846 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
851 void mathematica(MathArray const & dat, MathematicaStream & os)
854 extractStructure(ar);
855 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
856 (*it)->mathematica(os);
860 void mathmlize(MathArray const & dat, MathMLStream & os)
863 extractStructure(ar);
866 else if (ar.size() == 1)
870 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
871 (*it)->mathmlize(os);
881 string captureOutput(string const & cmd, string const & data)
883 string command = "echo '" + data + "' | " + cmd;
884 lyxerr << "calling: " << command << endl;
885 cmd_ret const ret = RunCommand(command);
889 string::size_type get_matching_brace(string const & str, string::size_type i)
892 string::size_type n = str.size();
894 i = str.find_first_of("{}", i+1);
895 if (i == string::npos) return i;
906 string::size_type get_matching_brace_back(string const & str, string::size_type i)
910 i = str.find_last_of("{}", i-1);
911 if (i == string::npos) return i;
922 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
927 string expr = STRCONV(os.str());
928 string const header = "SIMPSUM:true;";
931 for (int i = 0; i < 100; ++i) { // at most 100 attempts
932 // try to fix missing '*' the hard way
934 // > echo "2x;" | maxima
936 // (C1) Incorrect syntax: x is not an infix operator
940 lyxerr << "checking expr: '" << expr << "'\n";
941 string full = header + "tex(" + expr + ");";
942 out = captureOutput("maxima", full);
944 // leave loop if expression syntax is probably ok
945 if (out.find("Incorrect syntax") == string::npos)
948 // search line with "Incorrect syntax"
949 istringstream is(out.c_str());
953 if (line.find("Incorrect syntax") != string::npos)
957 // 2nd next line is the one with caret
960 string::size_type pos = line.find('^');
961 lyxerr << "found caret at pos: '" << pos << "'\n";
962 if (pos == string::npos || pos < 4)
963 break; // caret position not found
964 pos -= 4; // skip the "tex(" part
965 if (expr[pos] == '*')
966 break; // two '*' in a row are definitely bad
967 expr.insert(pos, "*");
970 std::vector<string> tmp = getVectorFromString(out, "$$");
974 out = subst(tmp[1],"\\>", "");
975 lyxerr << "out: '" << out << "'\n";
977 // Ugly code that tries to make the result prettier
979 string::size_type i = out.find("\\mathchoice");
980 while (i != string::npos) {
981 string::size_type j = get_matching_brace(out, i + 12);
982 string::size_type k = get_matching_brace(out, j + 1);
983 k = get_matching_brace(out, k + 1);
984 k = get_matching_brace(out, k + 1);
985 string mid = out.substr(i + 13,j - i - 13);
986 if (mid.find("\\over") != string::npos)
987 mid = '{' + mid + '}';
988 out = out.substr(0,i)
991 //lyxerr << "out: " << out << endl;
992 i = out.find("\\mathchoice", i);
996 i = out.find("\\over");
997 while (i != string::npos) {
998 string::size_type j = get_matching_brace_back(out, i - 1);
999 if (j == string::npos || j == 0) break;
1000 string::size_type k = get_matching_brace(out, i + 5);
1001 if (k == string::npos || k + 1 == out.size()) break;
1002 out = out.substr(0,j - 1)
1004 + out.substr(j,i - j)
1005 + out.substr(i + 5,k - i - 4)
1006 + out.substr(k + 2);
1007 //lyxerr << "out: " << out << endl;
1008 i = out.find("\\over", i + 4);
1011 mathed_parse_cell(res, out);
1016 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1018 string header = "readlib(latex):\n";
1020 // remove the \\it for variable names
1021 //"#`latex/csname_font` := `\\it `:"
1023 "`latex/csname_font` := ``:\n";
1025 // export matrices in (...) instead of [...]
1027 "`latex/latex/matrix` := "
1028 "subs(`[`=`(`, `]`=`)`,"
1029 "eval(`latex/latex/matrix`)):\n";
1031 // replace \\cdots with proper '*'
1033 "`latex/latex/*` := "
1034 "subs(`\\,`=`\\cdot `,"
1035 "eval(`latex/latex/*`)):\n";
1037 // remove spurious \\noalign{\\medskip} in matrix output
1039 "`latex/latex/matrix`:= "
1040 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1041 "eval(`latex/latex/matrix`)):\n";
1043 //"#`latex/latex/symbol` "
1044 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1046 string trailer = "quit;";
1050 string expr = STRCONV(os.str());
1051 lyxerr << "ar: '" << ar << "'\n";
1052 lyxerr << "ms: '" << os.str() << "'\n";
1054 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1055 // try to fix missing '*' the hard way by using mint
1057 // ... > echo "1A;" | mint -i 1 -S -s -q
1060 // Probably missing an operator such as * p
1062 lyxerr << "checking expr: '" << expr << "'\n";
1063 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1065 break; // expression syntax is ok
1066 istringstream is(out.c_str());
1069 if (line.find("on line") != 0)
1070 break; // error message not identified
1072 string::size_type pos = line.find('^');
1073 if (pos == string::npos || pos < 15)
1074 break; // caret position not found
1075 pos -= 15; // skip the "on line ..." part
1076 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1077 break; // two '*' in a row are definitely bad
1078 expr.insert(pos, 1, '*');
1081 string full = "latex(" + extra + '(' + expr + "));";
1082 string out = captureOutput("maple -q", header + full + trailer);
1088 mathed_parse_cell(res, out);
1093 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1096 OctaveStream vs(os);
1098 string expr = STRCONV(os.str());
1101 lyxerr << "pipe: ar: '" << ar << "'\n";
1102 lyxerr << "pipe: expr: '" << expr << "'\n";
1104 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1106 // try to fix missing '*' the hard way
1108 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1111 lyxerr << "checking expr: '" << expr << "'\n";
1112 out = captureOutput("octave -q 2>&1", expr);
1113 lyxerr << "checking out: '" << out << "'\n";
1115 // leave loop if expression syntax is probably ok
1116 if (out.find("parse error:") == string::npos)
1119 // search line with single caret
1120 istringstream is(out.c_str());
1124 lyxerr << "skipping line: '" << line << "'\n";
1125 if (line.find(">>> ") != string::npos)
1129 // found line with error, next line is the one with caret
1131 string::size_type pos = line.find('^');
1132 lyxerr << "caret line: '" << line << "'\n";
1133 lyxerr << "found caret at pos: '" << pos << "'\n";
1134 if (pos == string::npos || pos < 4)
1135 break; // caret position not found
1136 pos -= 4; // skip the ">>> " part
1137 if (expr[pos] == '*')
1138 break; // two '*' in a row are definitely bad
1139 expr.insert(pos, 1, '*');
1146 out = out.substr(6);
1148 // parse output as matrix or single number
1149 MathAtom at(new MathArrayInset("array", out));
1150 MathArrayInset const * mat = at->asArrayInset();
1152 if (mat->ncols() == 1 && mat->nrows() == 1)
1153 res.append(mat->cell(0));
1155 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1156 res.back().nucleus()->cell(0).push_back(at);
1164 MathArray pipeThroughExtern(string const & lang, string const & extra,
1165 MathArray const & ar)
1167 if (lang == "octave")
1168 return pipeThroughOctave(extra, ar);
1170 if (lang == "maxima")
1171 return pipeThroughMaxima(extra, ar);
1173 if (lang == "maple")
1174 return pipeThroughMaple(extra, ar);
1176 // create normalized expression
1178 NormalStream ns(os);
1179 os << '[' << extra << ' ';
1182 string data = STRCONV(os.str());
1184 // search external script
1185 string file = LibFileSearch("mathed", "extern_" + lang);
1187 lyxerr << "converter to '" << lang << "' not found" << endl;
1191 // run external sript
1192 string out = captureOutput(file, data);
1194 mathed_parse_cell(res, out);