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_arrayinset.h"
18 #include "math_charinset.h"
19 #include "math_deliminset.h"
20 #include "math_diffinset.h"
21 #include "math_exfuncinset.h"
22 #include "math_exintinset.h"
23 #include "math_fracinset.h"
24 #include "math_liminset.h"
25 #include "math_matrixinset.h"
26 #include "math_mathmlstream.h"
27 #include "math_numberinset.h"
28 #include "math_scriptinset.h"
29 #include "math_stringinset.h"
30 #include "math_symbolinset.h"
31 #include "math_parser.h"
34 #include "support/filetools.h"
35 #include "support/lstrings.h"
38 using namespace lyx::support;
41 using std::istringstream;
46 ostream & operator<<(ostream & os, MathArray const & ar)
54 // define a function for tests
55 typedef bool TestItemFunc(MathAtom const &);
57 // define a function for replacing subexpressions
58 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
62 // try to extract a super/subscript
63 // modify iterator position to point behind the thing
64 bool extractScript(MathArray & ar,
65 MathArray::iterator & pos, MathArray::iterator last)
67 // nothing to get here
71 // is this a scriptinset?
72 if (!(*pos)->asScriptInset())
75 // it is a scriptinset, use it.
82 // try to extract an "argument" to some function.
83 // returns position behind the argument
84 MathArray::iterator extractArgument(MathArray & ar,
85 MathArray::iterator pos, MathArray::iterator last, string const & = "")
87 // nothing to get here
91 // something deliminited _is_ an argument
92 if ((*pos)->asDelimInset()) {
97 // always take the first thing, no matter what it is
100 // go ahead if possible
105 // if the next item is a subscript, it most certainly belongs to the
107 extractScript(ar, pos, last);
111 // but it might be more than that.
112 // FIXME: not implemented
113 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
114 // // always take the first thing, no matter
116 // ar.push_back(*it);
124 // returns sequence of char with same code starting at it up to end
125 // it might be less, though...
127 (MathArray::const_iterator it, MathArray::const_iterator end)
130 for (; it != end && (*it)->asCharInset(); ++it)
131 s += (*it)->getChar();
136 void extractStrings(MathArray & ar)
138 //lyxerr << "\nStrings from: " << ar << endl;
139 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
140 if (!ar[i]->asCharInset())
142 string s = charSequence(ar.begin() + i, ar.end());
143 ar[i] = MathAtom(new MathStringInset(s));
144 ar.erase(i + 1, i + s.size());
146 //lyxerr << "\nStrings to: " << ar << endl;
150 void extractMatrices(MathArray & ar)
152 //lyxerr << "\nMatrices from: " << ar << endl;
153 // first pass for explicitly delimited stuff
154 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
155 if (!ar[i]->asDelimInset())
157 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
160 if (!arr.front()->asGridInset())
162 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
165 // second pass for AMS "pmatrix" etc
166 for (MathArray::size_type i = 0; i < ar.size(); ++i)
167 if (ar[i]->asAMSArrayInset())
168 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
169 //lyxerr << "\nMatrices to: " << ar << endl;
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, string const & 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 if (!testOpen(ar[i]))
247 // search end of sequence
248 MathArray::iterator it = ar.begin() + i;
249 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
253 // replace the original stuff by the new inset
254 ar[i] = replaceArg(MathArray(it + 1, jt));
255 ar.erase(it + 1, jt + 1);
262 // split scripts into seperate super- and subscript insets. sub goes in
266 void splitScripts(MathArray & ar)
268 //lyxerr << "\nScripts from: " << ar << endl;
269 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
270 // is this script inset?
271 if (!ar[i]->asScriptInset())
274 // no problem if we don't have both...
275 if (!ar[i]->asScriptInset()->hasUp())
277 if (!ar[i]->asScriptInset()->hasDown())
280 // create extra script inset and move superscript over
281 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
282 MathScriptInset * q = new MathScriptInset(true);
283 std::swap(q->up(), p->up());
284 p->removeScript(true);
286 // insert new inset behind
288 ar.insert(i, MathAtom(q));
290 //lyxerr << "\nScripts to: " << ar << endl;
298 void extractExps(MathArray & ar)
300 //lyxerr << "\nExps from: " << ar << endl;
301 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
303 if (ar[i]->getChar() != 'e')
306 // we need an exponent but no subscript
307 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
308 if (!sup || sup->hasDown())
311 // create a proper exp-inset as replacement
312 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
315 //lyxerr << "\nExps to: " << ar << endl;
320 // extract det(...) from |matrix|
322 void extractDets(MathArray & ar)
324 //lyxerr << "\ndet from: " << ar << endl;
325 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
326 MathDelimInset const * del = (*it)->asDelimInset();
331 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
333 //lyxerr << "\ndet to: " << ar << endl;
341 bool isDigitOrSimilar(char c)
343 return ('0' <= c && c <= '9') || c == '.';
347 // returns sequence of digits
349 (MathArray::const_iterator it, MathArray::const_iterator end)
352 for (; it != end && (*it)->asCharInset(); ++it) {
353 if (!isDigitOrSimilar((*it)->getChar()))
355 s += (*it)->getChar();
361 void extractNumbers(MathArray & ar)
363 //lyxerr << "\nNumbers from: " << ar << endl;
364 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
365 if (!ar[i]->asCharInset())
367 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
370 string s = digitSequence(ar.begin() + i, ar.end());
372 ar[i] = MathAtom(new MathNumberInset(s));
373 ar.erase(i + 1, i + s.size());
375 //lyxerr << "\nNumbers to: " << ar << endl;
381 // search deliminiters
384 bool testOpenParan(MathAtom const & at)
386 return testString(at, "(");
390 bool testCloseParan(MathAtom const & at)
392 return testString(at, ")");
396 MathAtom replaceDelims(const MathArray & ar)
398 return MathAtom(new MathDelimInset("(", ")", ar));
402 // replace '('...')' sequences by a real MathDelimInset
403 void extractDelims(MathArray & ar)
405 //lyxerr << "\nDelims from: " << ar << endl;
406 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
407 //lyxerr << "\nDelims to: " << ar << endl;
413 // search well-known functions
417 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
418 // assume 'extractDelims' ran before
419 void extractFunctions(MathArray & ar)
421 // we need at least two items...
425 //lyxerr << "\nFunctions from: " << ar << endl;
426 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
427 MathArray::iterator it = ar.begin() + i;
428 MathArray::iterator jt = it + 1;
432 if ((*it)->asUnknownInset()) {
433 // it certainly is if it is well known...
434 name = (*it)->name();
436 // is this a user defined function?
437 // it it probably not, if it doesn't have a name.
438 if (!extractString(*it, name))
440 // it is not if it has no argument
443 // guess so, if this is followed by
444 // a DelimInset with a single item in the cell
445 MathDelimInset const * del = (*jt)->asDelimInset();
446 if (!del || del->cell(0).size() != 1)
448 // fall trough into main branch
451 // do we have an exponent like in
452 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
454 extractScript(exp, jt, ar.end());
456 // create a proper inset as replacement
457 MathExFuncInset * p = new MathExFuncInset(name);
459 // jt points to the "argument". Get hold of this.
460 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
462 // replace the function name by a real function inset
465 // remove the source of the argument from the array
466 ar.erase(it + 1, st);
468 // re-insert exponent
469 ar.insert(i + 1, exp);
470 //lyxerr << "\nFunctions to: " << ar << endl;
479 bool testSymbol(MathAtom const & at, string const & name)
481 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
485 bool testIntSymbol(MathAtom const & at)
487 return testSymbol(at, "int");
491 bool testIntegral(MathAtom const & at)
495 ( at->asScriptInset()
496 && at->asScriptInset()->nuc().size()
497 && testIntSymbol(at->asScriptInset()->nuc().back()) );
502 bool testIntDiff(MathAtom const & at)
504 return testString(at, "d");
508 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
509 // assume 'extractDelims' ran before
510 void extractIntegrals(MathArray & ar)
512 // we need at least three items...
516 //lyxerr << "\nIntegrals from: " << ar << endl;
517 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
518 MathArray::iterator it = ar.begin() + i;
521 MathArray::iterator jt =
522 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
524 // something sensible found?
528 // is this a integral name?
529 if (!testIntegral(*it))
532 // core ist part from behind the scripts to the 'd'
533 MathExIntInset * p = new MathExIntInset("int");
535 // handle scripts if available
536 if (!testIntSymbol(*it)) {
537 p->cell(2) = (*it)->asScriptInset()->down();
538 p->cell(3) = (*it)->asScriptInset()->up();
540 p->cell(0) = MathArray(it + 1, jt);
542 // use the "thing" behind the 'd' as differential
543 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
546 ar.erase(it + 1, tt);
549 //lyxerr << "\nIntegrals to: " << ar << endl;
558 bool testEqualSign(MathAtom const & at)
560 return testString(at, "=");
564 bool testSumSymbol(MathAtom const & p)
566 return testSymbol(p, "sum");
570 bool testSum(MathAtom const & at)
574 ( at->asScriptInset()
575 && at->asScriptInset()->nuc().size()
576 && testSumSymbol(at->asScriptInset()->nuc().back()) );
580 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
581 // assume 'extractDelims' ran before
582 void extractSums(MathArray & ar)
584 // we need at least two items...
588 //lyxerr << "\nSums from: " << ar << endl;
589 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
590 MathArray::iterator it = ar.begin() + i;
592 // is this a sum name?
596 // create a proper inset as replacement
597 MathExIntInset * p = new MathExIntInset("sum");
599 // collect lower bound and summation index
600 MathScriptInset const * sub = ar[i]->asScriptInset();
601 if (sub && sub->hasDown()) {
602 // try to figure out the summation index from the subscript
603 MathArray const & ar = sub->down();
604 MathArray::const_iterator xt =
605 find_if(ar.begin(), ar.end(), &testEqualSign);
606 if (xt != ar.end()) {
607 // we found a '=', use everything in front of that as index,
608 // and everything behind as lower index
609 p->cell(1) = MathArray(ar.begin(), xt);
610 p->cell(2) = MathArray(xt + 1, ar.end());
612 // use everything as summation index, don't use scripts.
617 // collect upper bound
618 if (sub && sub->hasUp())
619 p->cell(3) = sub->up();
621 // use something behind the script as core
622 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
625 ar.erase(it + 1, tt);
628 //lyxerr << "\nSums to: " << ar << endl;
633 // search differential stuff
636 // tests for 'd' or '\partial'
637 bool testDiffItem(MathAtom const & at)
639 return testString(at, "d");
643 bool testDiffArray(MathArray const & ar)
645 return ar.size() && testDiffItem(ar.front());
649 bool testDiffFrac(MathAtom const & at)
653 && testDiffArray(at->asFracInset()->cell(0))
654 && testDiffArray(at->asFracInset()->cell(1));
658 void extractDiff(MathArray & ar)
660 //lyxerr << "\nDiffs from: " << ar << endl;
661 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
662 MathArray::iterator it = ar.begin() + i;
664 // is this a "differential fraction"?
665 if (!testDiffFrac(*it))
668 MathFracInset const * f = (*it)->asFracInset();
670 lyxerr << "should not happen" << endl;
674 // create a proper diff inset
675 MathDiffInset * diff = new MathDiffInset;
677 // collect function, let jt point behind last used item
678 MathArray::iterator jt = it + 1;
680 MathArray const & numer = f->cell(0);
681 if (numer.size() > 1 && numer[1]->asScriptInset()) {
682 // this is something like d^n f(x) / d... or d^n / d...
685 if (numer.size() > 2)
686 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
688 jt = extractArgument(diff->cell(0), jt, ar.end());
690 // simply d f(x) / d... or d/d...
691 if (numer.size() > 1)
692 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
694 jt = extractArgument(diff->cell(0), jt, ar.end());
697 // collect denominator parts
698 MathArray const & denom = f->cell(1);
699 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
701 MathArray::const_iterator et
702 = find_if(dt + 1, denom.end(), &testDiffItem);
705 MathArray::const_iterator st = et - 1;
706 MathScriptInset const * script = (*st)->asScriptInset();
707 if (script && script->hasUp()) {
708 // things like d.../dx^n
710 if (extractNumber(script->up(), mult)) {
711 //lyxerr << "mult: " << mult << endl;
712 for (int i = 0; i < mult; ++i)
713 diff->addDer(MathArray(dt + 1, st));
717 diff->addDer(MathArray(dt + 1, et));
723 ar.erase(it + 1, jt);
724 *it = MathAtom(diff);
726 //lyxerr << "\nDiffs to: " << ar << endl;
735 bool testRightArrow(MathAtom const & at)
737 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
742 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
743 // assume 'extractDelims' ran before
744 void extractLims(MathArray & ar)
746 // we need at least three items...
750 //lyxerr << "\nLimits from: " << ar << endl;
751 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
752 MathArray::iterator it = ar.begin() + i;
754 // is this a limit function?
755 if (!testSymbol(*it, "lim"))
758 // the next one must be a subscript (without superscript)
759 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
760 if (!sub || !sub->hasDown() || sub->hasUp())
763 // and it must contain a -> symbol
764 MathArray const & s = sub->down();
765 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
769 // the -> splits the subscript int x and x0
770 MathArray x = MathArray(s.begin(), st);
771 MathArray x0 = MathArray(st + 1, s.end());
773 // use something behind the script as core
775 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
778 ar.erase(it + 1, tt);
780 // create a proper inset as replacement
781 *it = MathAtom(new MathLimInset(f, x, x0));
783 //lyxerr << "\nLimits to: " << ar << endl;
791 void extractStructure(MathArray & ar)
793 //lyxerr << "\nStructure from: " << ar << endl;
794 extractIntegrals(ar);
800 extractFunctions(ar);
806 //lyxerr << "\nStructure to: " << ar << endl;
810 void write(MathArray const & dat, WriteStream & wi)
814 wi.firstitem() = true;
815 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
817 wi.firstitem() = false;
822 void normalize(MathArray const & ar, NormalStream & os)
824 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
825 (*it)->normalize(os);
829 void octave(MathArray const & dat, OctaveStream & os)
832 extractStructure(ar);
833 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
838 void maple(MathArray const & dat, MapleStream & os)
841 extractStructure(ar);
842 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
847 void maxima(MathArray const & dat, MaximaStream & os)
850 extractStructure(ar);
851 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
856 void mathematica(MathArray const & dat, MathematicaStream & os)
859 extractStructure(ar);
860 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
861 (*it)->mathematica(os);
865 void mathmlize(MathArray const & dat, MathMLStream & os)
868 extractStructure(ar);
871 else if (ar.size() == 1)
875 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
876 (*it)->mathmlize(os);
886 string captureOutput(string const & cmd, string const & data)
888 string command = "echo '" + data + "' | " + cmd;
889 lyxerr << "calling: " << command << endl;
890 cmd_ret const ret = RunCommand(command);
894 string::size_type get_matching_brace(string const & str, string::size_type i)
897 string::size_type n = str.size();
899 i = str.find_first_of("{}", i+1);
900 if (i == string::npos) return i;
911 string::size_type get_matching_brace_back(string const & str, string::size_type i)
915 i = str.find_last_of("{}", i-1);
916 if (i == string::npos) return i;
927 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
932 string expr = STRCONV(os.str());
933 string const header = "SIMPSUM:true;";
936 for (int i = 0; i < 100; ++i) { // at most 100 attempts
937 // try to fix missing '*' the hard way
939 // > echo "2x;" | maxima
941 // (C1) Incorrect syntax: x is not an infix operator
945 lyxerr << "checking expr: '" << expr << "'" << endl;
946 string full = header + "tex(" + expr + ");";
947 out = captureOutput("maxima", full);
949 // leave loop if expression syntax is probably ok
950 if (out.find("Incorrect syntax") == string::npos)
953 // search line with "Incorrect syntax"
954 istringstream is(out.c_str());
958 if (line.find("Incorrect syntax") != string::npos)
962 // 2nd next line is the one with caret
965 string::size_type pos = line.find('^');
966 lyxerr << "found caret at pos: '" << pos << "'" << endl;
967 if (pos == string::npos || pos < 4)
968 break; // caret position not found
969 pos -= 4; // skip the "tex(" part
970 if (expr[pos] == '*')
971 break; // two '*' in a row are definitely bad
972 expr.insert(pos, "*");
975 std::vector<string> tmp = getVectorFromString(out, "$$");
979 out = subst(tmp[1],"\\>", "");
980 lyxerr << "out: '" << out << "'" << endl;
982 // Ugly code that tries to make the result prettier
984 string::size_type i = out.find("\\mathchoice");
985 while (i != string::npos) {
986 string::size_type j = get_matching_brace(out, i + 12);
987 string::size_type k = get_matching_brace(out, j + 1);
988 k = get_matching_brace(out, k + 1);
989 k = get_matching_brace(out, k + 1);
990 string mid = out.substr(i + 13,j - i - 13);
991 if (mid.find("\\over") != string::npos)
992 mid = '{' + mid + '}';
993 out = out.substr(0,i)
996 //lyxerr << "out: " << out << endl;
997 i = out.find("\\mathchoice", i);
1001 i = out.find("\\over");
1002 while (i != string::npos) {
1003 string::size_type j = get_matching_brace_back(out, i - 1);
1004 if (j == string::npos || j == 0) break;
1005 string::size_type k = get_matching_brace(out, i + 5);
1006 if (k == string::npos || k + 1 == out.size()) break;
1007 out = out.substr(0,j - 1)
1009 + out.substr(j,i - j)
1010 + out.substr(i + 5,k - i - 4)
1011 + out.substr(k + 2);
1012 //lyxerr << "out: " << out << endl;
1013 i = out.find("\\over", i + 4);
1016 mathed_parse_cell(res, out);
1021 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1023 string header = "readlib(latex):\n";
1025 // remove the \\it for variable names
1026 //"#`latex/csname_font` := `\\it `:"
1028 "`latex/csname_font` := ``:\n";
1030 // export matrices in (...) instead of [...]
1032 "`latex/latex/matrix` := "
1033 "subs(`[`=`(`, `]`=`)`,"
1034 "eval(`latex/latex/matrix`)):\n";
1036 // replace \\cdots with proper '*'
1038 "`latex/latex/*` := "
1039 "subs(`\\,`=`\\cdot `,"
1040 "eval(`latex/latex/*`)):\n";
1042 // remove spurious \\noalign{\\medskip} in matrix output
1044 "`latex/latex/matrix`:= "
1045 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1046 "eval(`latex/latex/matrix`)):\n";
1048 //"#`latex/latex/symbol` "
1049 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1051 string trailer = "quit;";
1055 string expr = STRCONV(os.str());
1056 lyxerr << "ar: '" << ar << "'\n"
1057 << "ms: '" << os.str() << "'" << endl;
1059 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1060 // try to fix missing '*' the hard way by using mint
1062 // ... > echo "1A;" | mint -i 1 -S -s -q
1065 // Probably missing an operator such as * p
1067 lyxerr << "checking expr: '" << expr << "'" << endl;
1068 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1070 break; // expression syntax is ok
1071 istringstream is(out.c_str());
1074 if (line.find("on line") != 0)
1075 break; // error message not identified
1077 string::size_type pos = line.find('^');
1078 if (pos == string::npos || pos < 15)
1079 break; // caret position not found
1080 pos -= 15; // skip the "on line ..." part
1081 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1082 break; // two '*' in a row are definitely bad
1083 expr.insert(pos, 1, '*');
1086 string full = "latex(" + extra + '(' + expr + "));";
1087 string out = captureOutput("maple -q", header + full + trailer);
1093 mathed_parse_cell(res, out);
1098 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1101 OctaveStream vs(os);
1103 string expr = STRCONV(os.str());
1106 lyxerr << "pipe: ar: '" << ar << "'\n"
1107 << "pipe: expr: '" << expr << "'" << endl;
1109 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1111 // try to fix missing '*' the hard way
1113 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1116 lyxerr << "checking expr: '" << expr << "'" << endl;
1117 out = captureOutput("octave -q 2>&1", expr);
1118 lyxerr << "checking out: '" << out << "'" << endl;
1120 // leave loop if expression syntax is probably ok
1121 if (out.find("parse error:") == string::npos)
1124 // search line with single caret
1125 istringstream is(out.c_str());
1129 lyxerr << "skipping line: '" << line << "'" << endl;
1130 if (line.find(">>> ") != string::npos)
1134 // found line with error, next line is the one with caret
1136 string::size_type pos = line.find('^');
1137 lyxerr << "caret line: '" << line << "'" << endl;
1138 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1139 if (pos == string::npos || pos < 4)
1140 break; // caret position not found
1141 pos -= 4; // skip the ">>> " part
1142 if (expr[pos] == '*')
1143 break; // two '*' in a row are definitely bad
1144 expr.insert(pos, 1, '*');
1151 out = out.substr(6);
1153 // parse output as matrix or single number
1154 MathAtom at(new MathArrayInset("array", out));
1155 MathArrayInset const * mat = at->asArrayInset();
1157 if (mat->ncols() == 1 && mat->nrows() == 1)
1158 res.append(mat->cell(0));
1160 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1161 res.back().nucleus()->cell(0).push_back(at);
1169 MathArray pipeThroughExtern(string const & lang, string const & extra,
1170 MathArray const & ar)
1172 if (lang == "octave")
1173 return pipeThroughOctave(extra, ar);
1175 if (lang == "maxima")
1176 return pipeThroughMaxima(extra, ar);
1178 if (lang == "maple")
1179 return pipeThroughMaple(extra, ar);
1181 // create normalized expression
1183 NormalStream ns(os);
1184 os << '[' << extra << ' ';
1187 string data = STRCONV(os.str());
1189 // search external script
1190 string file = LibFileSearch("mathed", "extern_" + lang);
1192 lyxerr << "converter to '" << lang << "' not found" << endl;
1196 // run external sript
1197 string out = captureOutput(file, data);
1199 mathed_parse_cell(res, out);