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"
39 using namespace lyx::support;
42 using std::istringstream;
47 ostream & operator<<(ostream & os, MathArray const & ar)
55 // define a function for tests
56 typedef bool TestItemFunc(MathAtom const &);
58 // define a function for replacing subexpressions
59 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
63 // try to extract a super/subscript
64 // modify iterator position to point behind the thing
65 bool extractScript(MathArray & ar,
66 MathArray::iterator & pos, MathArray::iterator last)
68 // nothing to get here
72 // is this a scriptinset?
73 if (!(*pos)->asScriptInset())
76 // it is a scriptinset, use it.
83 // try to extract an "argument" to some function.
84 // returns position behind the argument
85 MathArray::iterator extractArgument(MathArray & ar,
86 MathArray::iterator pos, MathArray::iterator last, string const & = "")
88 // nothing to get here
92 // something deliminited _is_ an argument
93 if ((*pos)->asDelimInset()) {
98 // always take the first thing, no matter what it is
101 // go ahead if possible
106 // if the next item is a subscript, it most certainly belongs to the
108 extractScript(ar, pos, last);
112 // but it might be more than that.
113 // FIXME: not implemented
114 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
115 // // always take the first thing, no matter
117 // ar.push_back(*it);
125 // returns sequence of char with same code starting at it up to end
126 // it might be less, though...
128 (MathArray::const_iterator it, MathArray::const_iterator end)
131 for (; it != end && (*it)->asCharInset(); ++it)
132 s += (*it)->getChar();
137 void extractStrings(MathArray & ar)
139 //lyxerr << "\nStrings from: " << ar << endl;
140 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
141 if (!ar[i]->asCharInset())
143 string s = charSequence(ar.begin() + i, ar.end());
144 ar[i] = MathAtom(new MathStringInset(s));
145 ar.erase(i + 1, i + s.size());
147 //lyxerr << "\nStrings to: " << ar << endl;
151 void extractMatrices(MathArray & ar)
153 //lyxerr << "\nMatrices from: " << ar << endl;
154 // first pass for explicitly delimited stuff
155 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
156 if (!ar[i]->asDelimInset())
158 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
161 if (!arr.front()->asGridInset())
163 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
166 // second pass for AMS "pmatrix" etc
167 for (MathArray::size_type i = 0; i < ar.size(); ++i)
168 if (ar[i]->asAMSArrayInset())
169 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
170 //lyxerr << "\nMatrices to: " << ar << endl;
174 // convert this inset somehow to a string
175 bool extractString(MathAtom const & at, string & str)
178 str = string(1, at->getChar());
181 if (at->asStringInset()) {
182 str = at->asStringInset()->str();
189 // convert this inset somehow to a number
190 bool extractNumber(MathArray const & ar, int & i)
192 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
198 bool extractNumber(MathArray const & ar, double & d)
200 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
206 bool testString(MathAtom const & at, string const & str)
209 return extractString(at, s) && str == s;
213 // search end of nested sequence
214 MathArray::iterator endNestSearch(
215 MathArray::iterator it,
216 MathArray::iterator last,
217 TestItemFunc testOpen,
218 TestItemFunc testClose
221 for (int level = 0; it != last; ++it) {
233 // replace nested sequences by a real Insets
236 TestItemFunc testOpen,
237 TestItemFunc testClose,
238 ReplaceArgumentFunc replaceArg
241 // use indices rather than iterators for the loop because we are going
242 // to modify the array.
243 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
244 // check whether this is the begin of the sequence
245 if (!testOpen(ar[i]))
248 // search end of sequence
249 MathArray::iterator it = ar.begin() + i;
250 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
254 // replace the original stuff by the new inset
255 ar[i] = replaceArg(MathArray(it + 1, jt));
256 ar.erase(it + 1, jt + 1);
263 // split scripts into seperate super- and subscript insets. sub goes in
267 void splitScripts(MathArray & ar)
269 //lyxerr << "\nScripts from: " << ar << endl;
270 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
271 // is this script inset?
272 if (!ar[i]->asScriptInset())
275 // no problem if we don't have both...
276 if (!ar[i]->asScriptInset()->hasUp())
278 if (!ar[i]->asScriptInset()->hasDown())
281 // create extra script inset and move superscript over
282 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
283 MathScriptInset * q = new MathScriptInset(true);
284 std::swap(q->up(), p->up());
285 p->removeScript(true);
287 // insert new inset behind
289 ar.insert(i, MathAtom(q));
291 //lyxerr << "\nScripts to: " << ar << endl;
299 void extractExps(MathArray & ar)
301 //lyxerr << "\nExps from: " << ar << endl;
302 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
304 if (ar[i]->getChar() != 'e')
307 // we need an exponent but no subscript
308 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
309 if (!sup || sup->hasDown())
312 // create a proper exp-inset as replacement
313 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
316 //lyxerr << "\nExps to: " << ar << endl;
321 // extract det(...) from |matrix|
323 void extractDets(MathArray & ar)
325 //lyxerr << "\ndet from: " << ar << endl;
326 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
327 MathDelimInset const * del = (*it)->asDelimInset();
332 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
334 //lyxerr << "\ndet to: " << ar << endl;
342 bool isDigitOrSimilar(char c)
344 return ('0' <= c && c <= '9') || c == '.';
348 // returns sequence of digits
350 (MathArray::const_iterator it, MathArray::const_iterator end)
353 for (; it != end && (*it)->asCharInset(); ++it) {
354 if (!isDigitOrSimilar((*it)->getChar()))
356 s += (*it)->getChar();
362 void extractNumbers(MathArray & ar)
364 //lyxerr << "\nNumbers from: " << ar << endl;
365 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
366 if (!ar[i]->asCharInset())
368 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
371 string s = digitSequence(ar.begin() + i, ar.end());
373 ar[i] = MathAtom(new MathNumberInset(s));
374 ar.erase(i + 1, i + s.size());
376 //lyxerr << "\nNumbers to: " << ar << endl;
382 // search deliminiters
385 bool testOpenParan(MathAtom const & at)
387 return testString(at, "(");
391 bool testCloseParan(MathAtom const & at)
393 return testString(at, ")");
397 MathAtom replaceDelims(const MathArray & ar)
399 return MathAtom(new MathDelimInset("(", ")", ar));
403 // replace '('...')' sequences by a real MathDelimInset
404 void extractDelims(MathArray & ar)
406 //lyxerr << "\nDelims from: " << ar << endl;
407 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
408 //lyxerr << "\nDelims to: " << ar << endl;
414 // search well-known functions
418 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
419 // assume 'extractDelims' ran before
420 void extractFunctions(MathArray & ar)
422 // we need at least two items...
426 //lyxerr << "\nFunctions from: " << ar << endl;
427 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
428 MathArray::iterator it = ar.begin() + i;
429 MathArray::iterator jt = it + 1;
433 if ((*it)->asUnknownInset()) {
434 // it certainly is if it is well known...
435 name = (*it)->name();
437 // is this a user defined function?
438 // it it probably not, if it doesn't have a name.
439 if (!extractString(*it, name))
441 // it is not if it has no argument
444 // guess so, if this is followed by
445 // a DelimInset with a single item in the cell
446 MathDelimInset const * del = (*jt)->asDelimInset();
447 if (!del || del->cell(0).size() != 1)
449 // fall trough into main branch
452 // do we have an exponent like in
453 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
455 extractScript(exp, jt, ar.end());
457 // create a proper inset as replacement
458 MathExFuncInset * p = new MathExFuncInset(name);
460 // jt points to the "argument". Get hold of this.
461 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
463 // replace the function name by a real function inset
466 // remove the source of the argument from the array
467 ar.erase(it + 1, st);
469 // re-insert exponent
470 ar.insert(i + 1, exp);
471 //lyxerr << "\nFunctions to: " << ar << endl;
480 bool testSymbol(MathAtom const & at, string const & name)
482 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
486 bool testIntSymbol(MathAtom const & at)
488 return testSymbol(at, "int");
492 bool testIntegral(MathAtom const & at)
496 ( at->asScriptInset()
497 && at->asScriptInset()->nuc().size()
498 && testIntSymbol(at->asScriptInset()->nuc().back()) );
503 bool testIntDiff(MathAtom const & at)
505 return testString(at, "d");
509 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
510 // assume 'extractDelims' ran before
511 void extractIntegrals(MathArray & ar)
513 // we need at least three items...
517 //lyxerr << "\nIntegrals from: " << ar << endl;
518 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
519 MathArray::iterator it = ar.begin() + i;
522 MathArray::iterator jt =
523 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
525 // something sensible found?
529 // is this a integral name?
530 if (!testIntegral(*it))
533 // core ist part from behind the scripts to the 'd'
534 MathExIntInset * p = new MathExIntInset("int");
536 // handle scripts if available
537 if (!testIntSymbol(*it)) {
538 p->cell(2) = (*it)->asScriptInset()->down();
539 p->cell(3) = (*it)->asScriptInset()->up();
541 p->cell(0) = MathArray(it + 1, jt);
543 // use the "thing" behind the 'd' as differential
544 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
547 ar.erase(it + 1, tt);
550 //lyxerr << "\nIntegrals to: " << ar << endl;
559 bool testEqualSign(MathAtom const & at)
561 return testString(at, "=");
565 bool testSumSymbol(MathAtom const & p)
567 return testSymbol(p, "sum");
571 bool testSum(MathAtom const & at)
575 ( at->asScriptInset()
576 && at->asScriptInset()->nuc().size()
577 && testSumSymbol(at->asScriptInset()->nuc().back()) );
581 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
582 // assume 'extractDelims' ran before
583 void extractSums(MathArray & ar)
585 // we need at least two items...
589 //lyxerr << "\nSums from: " << ar << endl;
590 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
591 MathArray::iterator it = ar.begin() + i;
593 // is this a sum name?
597 // create a proper inset as replacement
598 MathExIntInset * p = new MathExIntInset("sum");
600 // collect lower bound and summation index
601 MathScriptInset const * sub = ar[i]->asScriptInset();
602 if (sub && sub->hasDown()) {
603 // try to figure out the summation index from the subscript
604 MathArray const & ar = sub->down();
605 MathArray::const_iterator xt =
606 find_if(ar.begin(), ar.end(), &testEqualSign);
607 if (xt != ar.end()) {
608 // we found a '=', use everything in front of that as index,
609 // and everything behind as lower index
610 p->cell(1) = MathArray(ar.begin(), xt);
611 p->cell(2) = MathArray(xt + 1, ar.end());
613 // use everything as summation index, don't use scripts.
618 // collect upper bound
619 if (sub && sub->hasUp())
620 p->cell(3) = sub->up();
622 // use something behind the script as core
623 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
626 ar.erase(it + 1, tt);
629 //lyxerr << "\nSums to: " << ar << endl;
634 // search differential stuff
637 // tests for 'd' or '\partial'
638 bool testDiffItem(MathAtom const & at)
640 return testString(at, "d");
644 bool testDiffArray(MathArray const & ar)
646 return ar.size() && testDiffItem(ar.front());
650 bool testDiffFrac(MathAtom const & at)
654 && testDiffArray(at->asFracInset()->cell(0))
655 && testDiffArray(at->asFracInset()->cell(1));
659 void extractDiff(MathArray & ar)
661 //lyxerr << "\nDiffs from: " << ar << endl;
662 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
663 MathArray::iterator it = ar.begin() + i;
665 // is this a "differential fraction"?
666 if (!testDiffFrac(*it))
669 MathFracInset const * f = (*it)->asFracInset();
671 lyxerr << "should not happen" << endl;
675 // create a proper diff inset
676 MathDiffInset * diff = new MathDiffInset;
678 // collect function, let jt point behind last used item
679 MathArray::iterator jt = it + 1;
681 MathArray const & numer = f->cell(0);
682 if (numer.size() > 1 && numer[1]->asScriptInset()) {
683 // this is something like d^n f(x) / d... or d^n / d...
686 if (numer.size() > 2)
687 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
689 jt = extractArgument(diff->cell(0), jt, ar.end());
691 // simply d f(x) / d... or d/d...
692 if (numer.size() > 1)
693 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
695 jt = extractArgument(diff->cell(0), jt, ar.end());
698 // collect denominator parts
699 MathArray const & denom = f->cell(1);
700 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
702 MathArray::const_iterator et
703 = find_if(dt + 1, denom.end(), &testDiffItem);
706 MathArray::const_iterator st = et - 1;
707 MathScriptInset const * script = (*st)->asScriptInset();
708 if (script && script->hasUp()) {
709 // things like d.../dx^n
711 if (extractNumber(script->up(), mult)) {
712 //lyxerr << "mult: " << mult << endl;
713 for (int i = 0; i < mult; ++i)
714 diff->addDer(MathArray(dt + 1, st));
718 diff->addDer(MathArray(dt + 1, et));
724 ar.erase(it + 1, jt);
725 *it = MathAtom(diff);
727 //lyxerr << "\nDiffs to: " << ar << endl;
736 bool testRightArrow(MathAtom const & at)
738 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
743 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
744 // assume 'extractDelims' ran before
745 void extractLims(MathArray & ar)
747 // we need at least three items...
751 //lyxerr << "\nLimits from: " << ar << endl;
752 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
753 MathArray::iterator it = ar.begin() + i;
755 // is this a limit function?
756 if (!testSymbol(*it, "lim"))
759 // the next one must be a subscript (without superscript)
760 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
761 if (!sub || !sub->hasDown() || sub->hasUp())
764 // and it must contain a -> symbol
765 MathArray const & s = sub->down();
766 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
770 // the -> splits the subscript int x and x0
771 MathArray x = MathArray(s.begin(), st);
772 MathArray x0 = MathArray(st + 1, s.end());
774 // use something behind the script as core
776 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
779 ar.erase(it + 1, tt);
781 // create a proper inset as replacement
782 *it = MathAtom(new MathLimInset(f, x, x0));
784 //lyxerr << "\nLimits to: " << ar << endl;
792 void extractStructure(MathArray & ar)
794 //lyxerr << "\nStructure from: " << ar << endl;
795 extractIntegrals(ar);
801 extractFunctions(ar);
807 //lyxerr << "\nStructure to: " << ar << endl;
811 void write(MathArray const & dat, WriteStream & wi)
815 wi.firstitem() = true;
816 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
818 wi.firstitem() = false;
823 void normalize(MathArray const & ar, NormalStream & os)
825 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
826 (*it)->normalize(os);
830 void octave(MathArray const & dat, OctaveStream & os)
833 extractStructure(ar);
834 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
839 void maple(MathArray const & dat, MapleStream & os)
842 extractStructure(ar);
843 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
848 void maxima(MathArray const & dat, MaximaStream & os)
851 extractStructure(ar);
852 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
857 void mathematica(MathArray const & dat, MathematicaStream & os)
860 extractStructure(ar);
861 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
862 (*it)->mathematica(os);
866 void mathmlize(MathArray const & dat, MathMLStream & os)
869 extractStructure(ar);
872 else if (ar.size() == 1)
876 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
877 (*it)->mathmlize(os);
887 string captureOutput(string const & cmd, string const & data)
889 string command = "echo '" + data + "' | " + cmd;
890 lyxerr << "calling: " << command << endl;
891 cmd_ret const ret = RunCommand(command);
895 string::size_type get_matching_brace(string const & str, string::size_type i)
898 string::size_type n = str.size();
900 i = str.find_first_of("{}", i+1);
901 if (i == string::npos) return i;
912 string::size_type get_matching_brace_back(string const & str, string::size_type i)
916 i = str.find_last_of("{}", i-1);
917 if (i == string::npos) return i;
928 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
933 string expr = STRCONV(os.str());
934 string const header = "SIMPSUM:true;";
937 for (int i = 0; i < 100; ++i) { // at most 100 attempts
938 // try to fix missing '*' the hard way
940 // > echo "2x;" | maxima
942 // (C1) Incorrect syntax: x is not an infix operator
946 lyxerr << "checking expr: '" << expr << "'" << endl;
947 string full = header + "tex(" + expr + ");";
948 out = captureOutput("maxima", full);
950 // leave loop if expression syntax is probably ok
951 if (out.find("Incorrect syntax") == string::npos)
954 // search line with "Incorrect syntax"
955 istringstream is(out.c_str());
959 if (line.find("Incorrect syntax") != string::npos)
963 // 2nd next line is the one with caret
966 string::size_type pos = line.find('^');
967 lyxerr << "found caret at pos: '" << pos << "'" << endl;
968 if (pos == string::npos || pos < 4)
969 break; // caret position not found
970 pos -= 4; // skip the "tex(" part
971 if (expr[pos] == '*')
972 break; // two '*' in a row are definitely bad
973 expr.insert(pos, "*");
976 std::vector<string> tmp = getVectorFromString(out, "$$");
980 out = subst(tmp[1],"\\>", "");
981 lyxerr << "out: '" << out << "'" << endl;
983 // Ugly code that tries to make the result prettier
985 string::size_type i = out.find("\\mathchoice");
986 while (i != string::npos) {
987 string::size_type j = get_matching_brace(out, i + 12);
988 string::size_type k = get_matching_brace(out, j + 1);
989 k = get_matching_brace(out, k + 1);
990 k = get_matching_brace(out, k + 1);
991 string mid = out.substr(i + 13,j - i - 13);
992 if (mid.find("\\over") != string::npos)
993 mid = '{' + mid + '}';
994 out = out.substr(0,i)
997 //lyxerr << "out: " << out << endl;
998 i = out.find("\\mathchoice", i);
1002 i = out.find("\\over");
1003 while (i != string::npos) {
1004 string::size_type j = get_matching_brace_back(out, i - 1);
1005 if (j == string::npos || j == 0) break;
1006 string::size_type k = get_matching_brace(out, i + 5);
1007 if (k == string::npos || k + 1 == out.size()) break;
1008 out = out.substr(0,j - 1)
1010 + out.substr(j,i - j)
1011 + out.substr(i + 5,k - i - 4)
1012 + out.substr(k + 2);
1013 //lyxerr << "out: " << out << endl;
1014 i = out.find("\\over", i + 4);
1017 mathed_parse_cell(res, out);
1022 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1024 string header = "readlib(latex):\n";
1026 // remove the \\it for variable names
1027 //"#`latex/csname_font` := `\\it `:"
1029 "`latex/csname_font` := ``:\n";
1031 // export matrices in (...) instead of [...]
1033 "`latex/latex/matrix` := "
1034 "subs(`[`=`(`, `]`=`)`,"
1035 "eval(`latex/latex/matrix`)):\n";
1037 // replace \\cdots with proper '*'
1039 "`latex/latex/*` := "
1040 "subs(`\\,`=`\\cdot `,"
1041 "eval(`latex/latex/*`)):\n";
1043 // remove spurious \\noalign{\\medskip} in matrix output
1045 "`latex/latex/matrix`:= "
1046 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1047 "eval(`latex/latex/matrix`)):\n";
1049 //"#`latex/latex/symbol` "
1050 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1052 string trailer = "quit;";
1056 string expr = STRCONV(os.str());
1057 lyxerr << "ar: '" << ar << "'\n"
1058 << "ms: '" << os.str() << "'" << endl;
1060 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1061 // try to fix missing '*' the hard way by using mint
1063 // ... > echo "1A;" | mint -i 1 -S -s -q
1066 // Probably missing an operator such as * p
1068 lyxerr << "checking expr: '" << expr << "'" << endl;
1069 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1071 break; // expression syntax is ok
1072 istringstream is(out.c_str());
1075 if (line.find("on line") != 0)
1076 break; // error message not identified
1078 string::size_type pos = line.find('^');
1079 if (pos == string::npos || pos < 15)
1080 break; // caret position not found
1081 pos -= 15; // skip the "on line ..." part
1082 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1083 break; // two '*' in a row are definitely bad
1084 expr.insert(pos, 1, '*');
1087 string full = "latex(" + extra + '(' + expr + "));";
1088 string out = captureOutput("maple -q", header + full + trailer);
1094 mathed_parse_cell(res, out);
1099 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1102 OctaveStream vs(os);
1104 string expr = STRCONV(os.str());
1107 lyxerr << "pipe: ar: '" << ar << "'\n"
1108 << "pipe: expr: '" << expr << "'" << endl;
1110 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1112 // try to fix missing '*' the hard way
1114 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1117 lyxerr << "checking expr: '" << expr << "'" << endl;
1118 out = captureOutput("octave -q 2>&1", expr);
1119 lyxerr << "checking out: '" << out << "'" << endl;
1121 // leave loop if expression syntax is probably ok
1122 if (out.find("parse error:") == string::npos)
1125 // search line with single caret
1126 istringstream is(out.c_str());
1130 lyxerr << "skipping line: '" << line << "'" << endl;
1131 if (line.find(">>> ") != string::npos)
1135 // found line with error, next line is the one with caret
1137 string::size_type pos = line.find('^');
1138 lyxerr << "caret line: '" << line << "'" << endl;
1139 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1140 if (pos == string::npos || pos < 4)
1141 break; // caret position not found
1142 pos -= 4; // skip the ">>> " part
1143 if (expr[pos] == '*')
1144 break; // two '*' in a row are definitely bad
1145 expr.insert(pos, 1, '*');
1152 out = out.substr(6);
1154 // parse output as matrix or single number
1155 MathAtom at(new MathArrayInset("array", out));
1156 MathArrayInset const * mat = at->asArrayInset();
1158 if (mat->ncols() == 1 && mat->nrows() == 1)
1159 res.append(mat->cell(0));
1161 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1162 res.back().nucleus()->cell(0).push_back(at);
1170 MathArray pipeThroughExtern(string const & lang, string const & extra,
1171 MathArray const & ar)
1173 if (lang == "octave")
1174 return pipeThroughOctave(extra, ar);
1176 if (lang == "maxima")
1177 return pipeThroughMaxima(extra, ar);
1179 if (lang == "maple")
1180 return pipeThroughMaple(extra, ar);
1182 // create normalized expression
1184 NormalStream ns(os);
1185 os << '[' << extra << ' ';
1188 string data = STRCONV(os.str());
1190 // search external script
1191 string file = LibFileSearch("mathed", "extern_" + lang);
1193 lyxerr << "converter to '" << lang << "' not found" << endl;
1197 // run external sript
1198 string out = captureOutput(file, data);
1200 mathed_parse_cell(res, out);