3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
11 // This file contains most of the magic that extracts "context
12 // information" from the unstructered layout-oriented stuff in an
17 #include "math_extern.h"
18 #include "math_arrayinset.h"
19 #include "math_charinset.h"
20 #include "math_deliminset.h"
21 #include "math_data.h"
22 #include "math_diffinset.h"
23 #include "math_exfuncinset.h"
24 #include "math_exintinset.h"
25 #include "math_fracinset.h"
26 #include "math_liminset.h"
27 #include "math_matrixinset.h"
28 #include "math_mathmlstream.h"
29 #include "math_numberinset.h"
30 #include "math_scriptinset.h"
31 #include "math_stringinset.h"
32 #include "math_symbolinset.h"
33 #include "math_parser.h"
34 #include "support/std_sstream.h"
36 #include "support/filetools.h"
37 #include "support/lstrings.h"
41 using namespace lyx::support;
44 using std::istringstream;
45 using std::ostringstream;
50 ostream & operator<<(ostream & os, MathArray const & ar)
58 // define a function for tests
59 typedef bool TestItemFunc(MathAtom const &);
61 // define a function for replacing subexpressions
62 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
66 // try to extract a super/subscript
67 // modify iterator position to point behind the thing
68 bool extractScript(MathArray & ar,
69 MathArray::iterator & pos, MathArray::iterator last)
71 // nothing to get here
75 // is this a scriptinset?
76 if (!(*pos)->asScriptInset())
79 // it is a scriptinset, use it.
86 // try to extract an "argument" to some function.
87 // returns position behind the argument
88 MathArray::iterator extractArgument(MathArray & ar,
89 MathArray::iterator pos, MathArray::iterator last, string const & = "")
91 // nothing to get here
95 // something deliminited _is_ an argument
96 if ((*pos)->asDelimInset()) {
101 // always take the first thing, no matter what it is
104 // go ahead if possible
109 // if the next item is a subscript, it most certainly belongs to the
111 extractScript(ar, pos, last);
115 // but it might be more than that.
116 // FIXME: not implemented
117 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
118 // // always take the first thing, no matter
120 // ar.push_back(*it);
128 // returns sequence of char with same code starting at it up to end
129 // it might be less, though...
131 (MathArray::const_iterator it, MathArray::const_iterator end)
134 for (; it != end && (*it)->asCharInset(); ++it)
135 s += (*it)->getChar();
140 void extractStrings(MathArray & ar)
142 //lyxerr << "\nStrings from: " << ar << endl;
143 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
144 if (!ar[i]->asCharInset())
146 string s = charSequence(ar.begin() + i, ar.end());
147 ar[i] = MathAtom(new MathStringInset(s));
148 ar.erase(i + 1, i + s.size());
150 //lyxerr << "\nStrings to: " << ar << endl;
154 void extractMatrices(MathArray & ar)
156 //lyxerr << "\nMatrices from: " << ar << endl;
157 // first pass for explicitly delimited stuff
158 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
159 if (!ar[i]->asDelimInset())
161 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
164 if (!arr.front()->asGridInset())
166 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
169 // second pass for AMS "pmatrix" etc
170 for (MathArray::size_type i = 0; i < ar.size(); ++i)
171 if (ar[i]->asAMSArrayInset())
172 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
173 //lyxerr << "\nMatrices to: " << ar << endl;
177 // convert this inset somehow to a string
178 bool extractString(MathAtom const & at, string & str)
181 str = string(1, at->getChar());
184 if (at->asStringInset()) {
185 str = at->asStringInset()->str();
192 // convert this inset somehow to a number
193 bool extractNumber(MathArray const & ar, int & i)
195 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
201 bool extractNumber(MathArray const & ar, double & d)
203 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
209 bool testString(MathAtom const & at, string const & str)
212 return extractString(at, s) && str == s;
216 // search end of nested sequence
217 MathArray::iterator endNestSearch(
218 MathArray::iterator it,
219 MathArray::iterator last,
220 TestItemFunc testOpen,
221 TestItemFunc testClose
224 for (int level = 0; it != last; ++it) {
236 // replace nested sequences by a real Insets
239 TestItemFunc testOpen,
240 TestItemFunc testClose,
241 ReplaceArgumentFunc replaceArg
244 // use indices rather than iterators for the loop because we are going
245 // to modify the array.
246 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
247 // check whether this is the begin of the sequence
248 if (!testOpen(ar[i]))
251 // search end of sequence
252 MathArray::iterator it = ar.begin() + i;
253 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
257 // replace the original stuff by the new inset
258 ar[i] = replaceArg(MathArray(it + 1, jt));
259 ar.erase(it + 1, jt + 1);
266 // split scripts into seperate super- and subscript insets. sub goes in
270 void splitScripts(MathArray & ar)
272 //lyxerr << "\nScripts from: " << ar << endl;
273 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
274 // is this script inset?
275 if (!ar[i]->asScriptInset())
278 // no problem if we don't have both...
279 if (!ar[i]->asScriptInset()->hasUp())
281 if (!ar[i]->asScriptInset()->hasDown())
284 // create extra script inset and move superscript over
285 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
286 MathScriptInset * q = new MathScriptInset(true);
287 std::swap(q->up(), p->up());
288 p->removeScript(true);
290 // insert new inset behind
292 ar.insert(i, MathAtom(q));
294 //lyxerr << "\nScripts to: " << ar << endl;
302 void extractExps(MathArray & ar)
304 //lyxerr << "\nExps from: " << ar << endl;
305 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
307 if (ar[i]->getChar() != 'e')
310 // we need an exponent but no subscript
311 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
312 if (!sup || sup->hasDown())
315 // create a proper exp-inset as replacement
316 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
319 //lyxerr << "\nExps to: " << ar << endl;
324 // extract det(...) from |matrix|
326 void extractDets(MathArray & ar)
328 //lyxerr << "\ndet from: " << ar << endl;
329 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
330 MathDelimInset const * del = (*it)->asDelimInset();
335 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
337 //lyxerr << "\ndet to: " << ar << endl;
345 bool isDigitOrSimilar(char c)
347 return ('0' <= c && c <= '9') || c == '.';
351 // returns sequence of digits
353 (MathArray::const_iterator it, MathArray::const_iterator end)
356 for (; it != end && (*it)->asCharInset(); ++it) {
357 if (!isDigitOrSimilar((*it)->getChar()))
359 s += (*it)->getChar();
365 void extractNumbers(MathArray & ar)
367 //lyxerr << "\nNumbers from: " << ar << endl;
368 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
369 if (!ar[i]->asCharInset())
371 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
374 string s = digitSequence(ar.begin() + i, ar.end());
376 ar[i] = MathAtom(new MathNumberInset(s));
377 ar.erase(i + 1, i + s.size());
379 //lyxerr << "\nNumbers to: " << ar << endl;
385 // search deliminiters
388 bool testOpenParan(MathAtom const & at)
390 return testString(at, "(");
394 bool testCloseParan(MathAtom const & at)
396 return testString(at, ")");
400 MathAtom replaceDelims(const MathArray & ar)
402 return MathAtom(new MathDelimInset("(", ")", ar));
406 // replace '('...')' sequences by a real MathDelimInset
407 void extractDelims(MathArray & ar)
409 //lyxerr << "\nDelims from: " << ar << endl;
410 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
411 //lyxerr << "\nDelims to: " << ar << endl;
417 // search well-known functions
421 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
422 // assume 'extractDelims' ran before
423 void extractFunctions(MathArray & ar)
425 // we need at least two items...
429 //lyxerr << "\nFunctions from: " << ar << endl;
430 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
431 MathArray::iterator it = ar.begin() + i;
432 MathArray::iterator jt = it + 1;
436 if ((*it)->asUnknownInset()) {
437 // it certainly is if it is well known...
438 name = (*it)->name();
440 // is this a user defined function?
441 // it it probably not, if it doesn't have a name.
442 if (!extractString(*it, name))
444 // it is not if it has no argument
447 // guess so, if this is followed by
448 // a DelimInset with a single item in the cell
449 MathDelimInset const * del = (*jt)->asDelimInset();
450 if (!del || del->cell(0).size() != 1)
452 // fall trough into main branch
455 // do we have an exponent like in
456 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
458 extractScript(exp, jt, ar.end());
460 // create a proper inset as replacement
461 MathExFuncInset * p = new MathExFuncInset(name);
463 // jt points to the "argument". Get hold of this.
464 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
466 // replace the function name by a real function inset
469 // remove the source of the argument from the array
470 ar.erase(it + 1, st);
472 // re-insert exponent
473 ar.insert(i + 1, exp);
474 //lyxerr << "\nFunctions to: " << ar << endl;
483 bool testSymbol(MathAtom const & at, string const & name)
485 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
489 bool testIntSymbol(MathAtom const & at)
491 return testSymbol(at, "int");
495 bool testIntegral(MathAtom const & at)
499 ( at->asScriptInset()
500 && at->asScriptInset()->nuc().size()
501 && testIntSymbol(at->asScriptInset()->nuc().back()) );
506 bool testIntDiff(MathAtom const & at)
508 return testString(at, "d");
512 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
513 // assume 'extractDelims' ran before
514 void extractIntegrals(MathArray & ar)
516 // we need at least three items...
520 //lyxerr << "\nIntegrals from: " << ar << endl;
521 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
522 MathArray::iterator it = ar.begin() + i;
525 MathArray::iterator jt =
526 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
528 // something sensible found?
532 // is this a integral name?
533 if (!testIntegral(*it))
536 // core ist part from behind the scripts to the 'd'
537 MathExIntInset * p = new MathExIntInset("int");
539 // handle scripts if available
540 if (!testIntSymbol(*it)) {
541 p->cell(2) = (*it)->asScriptInset()->down();
542 p->cell(3) = (*it)->asScriptInset()->up();
544 p->cell(0) = MathArray(it + 1, jt);
546 // use the "thing" behind the 'd' as differential
547 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
550 ar.erase(it + 1, tt);
553 //lyxerr << "\nIntegrals to: " << ar << endl;
562 bool testEqualSign(MathAtom const & at)
564 return testString(at, "=");
568 bool testSumSymbol(MathAtom const & p)
570 return testSymbol(p, "sum");
574 bool testSum(MathAtom const & at)
578 ( at->asScriptInset()
579 && at->asScriptInset()->nuc().size()
580 && testSumSymbol(at->asScriptInset()->nuc().back()) );
584 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
585 // assume 'extractDelims' ran before
586 void extractSums(MathArray & ar)
588 // we need at least two items...
592 //lyxerr << "\nSums from: " << ar << endl;
593 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
594 MathArray::iterator it = ar.begin() + i;
596 // is this a sum name?
600 // create a proper inset as replacement
601 MathExIntInset * p = new MathExIntInset("sum");
603 // collect lower bound and summation index
604 MathScriptInset const * sub = ar[i]->asScriptInset();
605 if (sub && sub->hasDown()) {
606 // try to figure out the summation index from the subscript
607 MathArray const & ar = sub->down();
608 MathArray::const_iterator xt =
609 find_if(ar.begin(), ar.end(), &testEqualSign);
610 if (xt != ar.end()) {
611 // we found a '=', use everything in front of that as index,
612 // and everything behind as lower index
613 p->cell(1) = MathArray(ar.begin(), xt);
614 p->cell(2) = MathArray(xt + 1, ar.end());
616 // use everything as summation index, don't use scripts.
621 // collect upper bound
622 if (sub && sub->hasUp())
623 p->cell(3) = sub->up();
625 // use something behind the script as core
626 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
629 ar.erase(it + 1, tt);
632 //lyxerr << "\nSums to: " << ar << endl;
637 // search differential stuff
640 // tests for 'd' or '\partial'
641 bool testDiffItem(MathAtom const & at)
643 return testString(at, "d");
647 bool testDiffArray(MathArray const & ar)
649 return ar.size() && testDiffItem(ar.front());
653 bool testDiffFrac(MathAtom const & at)
657 && testDiffArray(at->asFracInset()->cell(0))
658 && testDiffArray(at->asFracInset()->cell(1));
662 void extractDiff(MathArray & ar)
664 //lyxerr << "\nDiffs from: " << ar << endl;
665 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
666 MathArray::iterator it = ar.begin() + i;
668 // is this a "differential fraction"?
669 if (!testDiffFrac(*it))
672 MathFracInset const * f = (*it)->asFracInset();
674 lyxerr << "should not happen" << endl;
678 // create a proper diff inset
679 MathDiffInset * diff = new MathDiffInset;
681 // collect function, let jt point behind last used item
682 MathArray::iterator jt = it + 1;
684 MathArray const & numer = f->cell(0);
685 if (numer.size() > 1 && numer[1]->asScriptInset()) {
686 // this is something like d^n f(x) / d... or d^n / d...
689 if (numer.size() > 2)
690 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
692 jt = extractArgument(diff->cell(0), jt, ar.end());
694 // simply d f(x) / d... or d/d...
695 if (numer.size() > 1)
696 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
698 jt = extractArgument(diff->cell(0), jt, ar.end());
701 // collect denominator parts
702 MathArray const & denom = f->cell(1);
703 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
705 MathArray::const_iterator et
706 = find_if(dt + 1, denom.end(), &testDiffItem);
709 MathArray::const_iterator st = et - 1;
710 MathScriptInset const * script = (*st)->asScriptInset();
711 if (script && script->hasUp()) {
712 // things like d.../dx^n
714 if (extractNumber(script->up(), mult)) {
715 //lyxerr << "mult: " << mult << endl;
716 for (int i = 0; i < mult; ++i)
717 diff->addDer(MathArray(dt + 1, st));
721 diff->addDer(MathArray(dt + 1, et));
727 ar.erase(it + 1, jt);
728 *it = MathAtom(diff);
730 //lyxerr << "\nDiffs to: " << ar << endl;
739 bool testRightArrow(MathAtom const & at)
741 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
746 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
747 // assume 'extractDelims' ran before
748 void extractLims(MathArray & ar)
750 // we need at least three items...
754 //lyxerr << "\nLimits from: " << ar << endl;
755 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
756 MathArray::iterator it = ar.begin() + i;
758 // is this a limit function?
759 if (!testSymbol(*it, "lim"))
762 // the next one must be a subscript (without superscript)
763 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
764 if (!sub || !sub->hasDown() || sub->hasUp())
767 // and it must contain a -> symbol
768 MathArray const & s = sub->down();
769 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
773 // the -> splits the subscript int x and x0
774 MathArray x = MathArray(s.begin(), st);
775 MathArray x0 = MathArray(st + 1, s.end());
777 // use something behind the script as core
779 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
782 ar.erase(it + 1, tt);
784 // create a proper inset as replacement
785 *it = MathAtom(new MathLimInset(f, x, x0));
787 //lyxerr << "\nLimits to: " << ar << endl;
795 void extractStructure(MathArray & ar)
797 //lyxerr << "\nStructure from: " << ar << endl;
798 extractIntegrals(ar);
804 extractFunctions(ar);
810 //lyxerr << "\nStructure to: " << ar << endl;
814 void write(MathArray const & dat, WriteStream & wi)
818 wi.firstitem() = true;
819 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
821 wi.firstitem() = false;
826 void normalize(MathArray const & ar, NormalStream & os)
828 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
829 (*it)->normalize(os);
833 void octave(MathArray const & dat, OctaveStream & os)
836 extractStructure(ar);
837 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
842 void maple(MathArray const & dat, MapleStream & os)
845 extractStructure(ar);
846 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
851 void maxima(MathArray const & dat, MaximaStream & os)
854 extractStructure(ar);
855 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
860 void mathematica(MathArray const & dat, MathematicaStream & os)
863 extractStructure(ar);
864 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
865 (*it)->mathematica(os);
869 void mathmlize(MathArray const & dat, MathMLStream & os)
872 extractStructure(ar);
875 else if (ar.size() == 1)
879 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
880 (*it)->mathmlize(os);
890 string captureOutput(string const & cmd, string const & data)
892 string command = "echo '" + data + "' | " + cmd;
893 lyxerr << "calling: " << command << endl;
894 cmd_ret const ret = RunCommand(command);
898 string::size_type get_matching_brace(string const & str, string::size_type i)
901 string::size_type n = str.size();
903 i = str.find_first_of("{}", i+1);
904 if (i == string::npos) return i;
915 string::size_type get_matching_brace_back(string const & str, string::size_type i)
919 i = str.find_last_of("{}", i-1);
920 if (i == string::npos) return i;
931 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
936 string expr = STRCONV(os.str());
937 string const header = "SIMPSUM:true;";
940 for (int i = 0; i < 100; ++i) { // at most 100 attempts
941 // try to fix missing '*' the hard way
943 // > echo "2x;" | maxima
945 // (C1) Incorrect syntax: x is not an infix operator
949 lyxerr << "checking expr: '" << expr << "'" << endl;
950 string full = header + "tex(" + expr + ");";
951 out = captureOutput("maxima", full);
953 // leave loop if expression syntax is probably ok
954 if (out.find("Incorrect syntax") == string::npos)
957 // search line with "Incorrect syntax"
958 istringstream is(out.c_str());
962 if (line.find("Incorrect syntax") != string::npos)
966 // 2nd next line is the one with caret
969 string::size_type pos = line.find('^');
970 lyxerr << "found caret at pos: '" << pos << "'" << endl;
971 if (pos == string::npos || pos < 4)
972 break; // caret position not found
973 pos -= 4; // skip the "tex(" part
974 if (expr[pos] == '*')
975 break; // two '*' in a row are definitely bad
976 expr.insert(pos, "*");
979 std::vector<string> tmp = getVectorFromString(out, "$$");
983 out = subst(tmp[1],"\\>", "");
984 lyxerr << "out: '" << out << "'" << endl;
986 // Ugly code that tries to make the result prettier
988 string::size_type i = out.find("\\mathchoice");
989 while (i != string::npos) {
990 string::size_type j = get_matching_brace(out, i + 12);
991 string::size_type k = get_matching_brace(out, j + 1);
992 k = get_matching_brace(out, k + 1);
993 k = get_matching_brace(out, k + 1);
994 string mid = out.substr(i + 13,j - i - 13);
995 if (mid.find("\\over") != string::npos)
996 mid = '{' + mid + '}';
997 out = out.substr(0,i)
1000 //lyxerr << "out: " << out << endl;
1001 i = out.find("\\mathchoice", i);
1005 i = out.find("\\over");
1006 while (i != string::npos) {
1007 string::size_type j = get_matching_brace_back(out, i - 1);
1008 if (j == string::npos || j == 0) break;
1009 string::size_type k = get_matching_brace(out, i + 5);
1010 if (k == string::npos || k + 1 == out.size()) break;
1011 out = out.substr(0,j - 1)
1013 + out.substr(j,i - j)
1014 + out.substr(i + 5,k - i - 4)
1015 + out.substr(k + 2);
1016 //lyxerr << "out: " << out << endl;
1017 i = out.find("\\over", i + 4);
1020 mathed_parse_cell(res, out);
1025 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1027 string header = "readlib(latex):\n";
1029 // remove the \\it for variable names
1030 //"#`latex/csname_font` := `\\it `:"
1032 "`latex/csname_font` := ``:\n";
1034 // export matrices in (...) instead of [...]
1036 "`latex/latex/matrix` := "
1037 "subs(`[`=`(`, `]`=`)`,"
1038 "eval(`latex/latex/matrix`)):\n";
1040 // replace \\cdots with proper '*'
1042 "`latex/latex/*` := "
1043 "subs(`\\,`=`\\cdot `,"
1044 "eval(`latex/latex/*`)):\n";
1046 // remove spurious \\noalign{\\medskip} in matrix output
1048 "`latex/latex/matrix`:= "
1049 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1050 "eval(`latex/latex/matrix`)):\n";
1052 //"#`latex/latex/symbol` "
1053 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1055 string trailer = "quit;";
1059 string expr = STRCONV(os.str());
1060 lyxerr << "ar: '" << ar << "'\n"
1061 << "ms: '" << os.str() << "'" << endl;
1063 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1064 // try to fix missing '*' the hard way by using mint
1066 // ... > echo "1A;" | mint -i 1 -S -s -q
1069 // Probably missing an operator such as * p
1071 lyxerr << "checking expr: '" << expr << "'" << endl;
1072 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1074 break; // expression syntax is ok
1075 istringstream is(out.c_str());
1078 if (line.find("on line") != 0)
1079 break; // error message not identified
1081 string::size_type pos = line.find('^');
1082 if (pos == string::npos || pos < 15)
1083 break; // caret position not found
1084 pos -= 15; // skip the "on line ..." part
1085 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1086 break; // two '*' in a row are definitely bad
1087 expr.insert(pos, 1, '*');
1090 string full = "latex(" + extra + '(' + expr + "));";
1091 string out = captureOutput("maple -q", header + full + trailer);
1097 mathed_parse_cell(res, out);
1102 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1105 OctaveStream vs(os);
1107 string expr = STRCONV(os.str());
1110 lyxerr << "pipe: ar: '" << ar << "'\n"
1111 << "pipe: expr: '" << expr << "'" << endl;
1113 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1115 // try to fix missing '*' the hard way
1117 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1120 lyxerr << "checking expr: '" << expr << "'" << endl;
1121 out = captureOutput("octave -q 2>&1", expr);
1122 lyxerr << "checking out: '" << out << "'" << endl;
1124 // leave loop if expression syntax is probably ok
1125 if (out.find("parse error:") == string::npos)
1128 // search line with single caret
1129 istringstream is(out.c_str());
1133 lyxerr << "skipping line: '" << line << "'" << endl;
1134 if (line.find(">>> ") != string::npos)
1138 // found line with error, next line is the one with caret
1140 string::size_type pos = line.find('^');
1141 lyxerr << "caret line: '" << line << "'" << endl;
1142 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1143 if (pos == string::npos || pos < 4)
1144 break; // caret position not found
1145 pos -= 4; // skip the ">>> " part
1146 if (expr[pos] == '*')
1147 break; // two '*' in a row are definitely bad
1148 expr.insert(pos, 1, '*');
1155 out = out.substr(6);
1157 // parse output as matrix or single number
1158 MathAtom at(new MathArrayInset("array", out));
1159 MathArrayInset const * mat = at->asArrayInset();
1161 if (mat->ncols() == 1 && mat->nrows() == 1)
1162 res.append(mat->cell(0));
1164 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1165 res.back().nucleus()->cell(0).push_back(at);
1173 MathArray pipeThroughExtern(string const & lang, string const & extra,
1174 MathArray const & ar)
1176 if (lang == "octave")
1177 return pipeThroughOctave(extra, ar);
1179 if (lang == "maxima")
1180 return pipeThroughMaxima(extra, ar);
1182 if (lang == "maple")
1183 return pipeThroughMaple(extra, ar);
1185 // create normalized expression
1187 NormalStream ns(os);
1188 os << '[' << extra << ' ';
1191 string data = STRCONV(os.str());
1193 // search external script
1194 string file = LibFileSearch("mathed", "extern_" + lang);
1196 lyxerr << "converter to '" << lang << "' not found" << endl;
1200 // run external sript
1201 string out = captureOutput(file, data);
1203 mathed_parse_cell(res, out);