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"
35 #include "support/filetools.h"
36 #include "support/lstrings.h"
41 using lyx::support::cmd_ret;
42 using lyx::support::getVectorFromString;
43 using lyx::support::libFileSearch;
44 using lyx::support::runCommand;
45 using lyx::support::subst;
51 using std::istringstream;
53 using std::ostringstream;
58 ostream & operator<<(ostream & os, MathArray const & ar)
66 // define a function for tests
67 typedef bool TestItemFunc(MathAtom const &);
69 // define a function for replacing subexpressions
70 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
74 // try to extract a super/subscript
75 // modify iterator position to point behind the thing
76 bool extractScript(MathArray & ar,
77 MathArray::iterator & pos, MathArray::iterator last)
79 // nothing to get here
83 // is this a scriptinset?
84 if (!(*pos)->asScriptInset())
87 // it is a scriptinset, use it.
94 // try to extract an "argument" to some function.
95 // returns position behind the argument
96 MathArray::iterator extractArgument(MathArray & ar,
97 MathArray::iterator pos, MathArray::iterator last, string const & = "")
99 // nothing to get here
103 // something deliminited _is_ an argument
104 if ((*pos)->asDelimInset()) {
109 // always take the first thing, no matter what it is
112 // go ahead if possible
117 // if the next item is a subscript, it most certainly belongs to the
119 extractScript(ar, pos, last);
123 // but it might be more than that.
124 // FIXME: not implemented
125 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
126 // // always take the first thing, no matter
128 // ar.push_back(*it);
136 // returns sequence of char with same code starting at it up to end
137 // it might be less, though...
139 (MathArray::const_iterator it, MathArray::const_iterator end)
142 for (; it != end && (*it)->asCharInset(); ++it)
143 s += (*it)->getChar();
148 void extractStrings(MathArray & ar)
150 //lyxerr << "\nStrings from: " << ar << endl;
151 for (size_t i = 0; i < ar.size(); ++i) {
152 if (!ar[i]->asCharInset())
154 string s = charSequence(ar.begin() + i, ar.end());
155 ar[i] = MathAtom(new MathStringInset(s));
156 ar.erase(i + 1, i + s.size());
158 //lyxerr << "\nStrings to: " << ar << endl;
162 void extractMatrices(MathArray & ar)
164 //lyxerr << "\nMatrices from: " << ar << endl;
165 // first pass for explicitly delimited stuff
166 for (size_t i = 0; i < ar.size(); ++i) {
167 if (!ar[i]->asDelimInset())
169 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
172 if (!arr.front()->asGridInset())
174 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
177 // second pass for AMS "pmatrix" etc
178 for (size_t i = 0; i < ar.size(); ++i)
179 if (ar[i]->asAMSArrayInset())
180 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
181 //lyxerr << "\nMatrices to: " << ar << endl;
185 // convert this inset somehow to a string
186 bool extractString(MathAtom const & at, string & str)
189 str = string(1, at->getChar());
192 if (at->asStringInset()) {
193 str = at->asStringInset()->str();
200 // convert this inset somehow to a number
201 bool extractNumber(MathArray const & ar, int & i)
203 istringstream is(charSequence(ar.begin(), ar.end()));
209 bool extractNumber(MathArray const & ar, double & d)
211 istringstream is(charSequence(ar.begin(), ar.end()));
217 bool testString(MathAtom const & at, string const & str)
220 return extractString(at, s) && str == s;
224 // search end of nested sequence
225 MathArray::iterator endNestSearch(
226 MathArray::iterator it,
227 MathArray::iterator last,
228 TestItemFunc testOpen,
229 TestItemFunc testClose
232 for (int level = 0; it != last; ++it) {
244 // replace nested sequences by a real Insets
247 TestItemFunc testOpen,
248 TestItemFunc testClose,
249 ReplaceArgumentFunc replaceArg
252 // use indices rather than iterators for the loop because we are going
253 // to modify the array.
254 for (size_t i = 0; i < ar.size(); ++i) {
255 // check whether this is the begin of the sequence
256 if (!testOpen(ar[i]))
259 // search end of sequence
260 MathArray::iterator it = ar.begin() + i;
261 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
265 // replace the original stuff by the new inset
266 ar[i] = replaceArg(MathArray(it + 1, jt));
267 ar.erase(it + 1, jt + 1);
274 // split scripts into seperate super- and subscript insets. sub goes in
278 void splitScripts(MathArray & ar)
280 //lyxerr << "\nScripts from: " << ar << endl;
281 for (size_t i = 0; i < ar.size(); ++i) {
282 // is this script inset?
283 if (!ar[i]->asScriptInset())
286 // no problem if we don't have both...
287 if (!ar[i]->asScriptInset()->hasUp())
289 if (!ar[i]->asScriptInset()->hasDown())
292 // create extra script inset and move superscript over
293 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
294 auto_ptr<MathScriptInset> q(new MathScriptInset(true));
295 swap(q->up(), p->up());
296 p->removeScript(true);
298 // insert new inset behind
300 ar.insert(i, MathAtom(q.release()));
302 //lyxerr << "\nScripts to: " << ar << endl;
310 void extractExps(MathArray & ar)
312 //lyxerr << "\nExps from: " << ar << endl;
313 for (size_t i = 0; i + 1 < ar.size(); ++i) {
315 if (ar[i]->getChar() != 'e')
318 // we need an exponent but no subscript
319 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
320 if (!sup || sup->hasDown())
323 // create a proper exp-inset as replacement
324 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
327 //lyxerr << "\nExps to: " << ar << endl;
332 // extract det(...) from |matrix|
334 void extractDets(MathArray & ar)
336 //lyxerr << "\ndet from: " << ar << endl;
337 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
338 MathDelimInset const * del = (*it)->asDelimInset();
343 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
345 //lyxerr << "\ndet to: " << ar << endl;
353 bool isDigitOrSimilar(char c)
355 return ('0' <= c && c <= '9') || c == '.';
359 // returns sequence of digits
361 (MathArray::const_iterator it, MathArray::const_iterator end)
364 for (; it != end && (*it)->asCharInset(); ++it) {
365 if (!isDigitOrSimilar((*it)->getChar()))
367 s += (*it)->getChar();
373 void extractNumbers(MathArray & ar)
375 //lyxerr << "\nNumbers from: " << ar << endl;
376 for (size_t i = 0; i < ar.size(); ++i) {
377 if (!ar[i]->asCharInset())
379 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
382 string s = digitSequence(ar.begin() + i, ar.end());
384 ar[i] = MathAtom(new MathNumberInset(s));
385 ar.erase(i + 1, i + s.size());
387 //lyxerr << "\nNumbers to: " << ar << endl;
393 // search deliminiters
396 bool testOpenParen(MathAtom const & at)
398 return testString(at, "(");
402 bool testCloseParen(MathAtom const & at)
404 return testString(at, ")");
408 MathAtom replaceDelims(const MathArray & ar)
410 return MathAtom(new MathDelimInset("(", ")", ar));
414 // replace '('...')' sequences by a real MathDelimInset
415 void extractDelims(MathArray & ar)
417 //lyxerr << "\nDelims from: " << ar << endl;
418 replaceNested(ar, testOpenParen, testCloseParen, replaceDelims);
419 //lyxerr << "\nDelims to: " << ar << endl;
425 // search well-known functions
429 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
430 // assume 'extractDelims' ran before
431 void extractFunctions(MathArray & ar)
433 // we need at least two items...
437 //lyxerr << "\nFunctions from: " << ar << endl;
438 for (size_t i = 0; i + 1 < ar.size(); ++i) {
439 MathArray::iterator it = ar.begin() + i;
440 MathArray::iterator jt = it + 1;
444 if ((*it)->asUnknownInset()) {
445 // it certainly is if it is well known...
446 name = (*it)->name();
448 // is this a user defined function?
449 // it it probably not, if it doesn't have a name.
450 if (!extractString(*it, name))
452 // it is not if it has no argument
455 // guess so, if this is followed by
456 // a DelimInset with a single item in the cell
457 MathDelimInset const * del = (*jt)->asDelimInset();
458 if (!del || del->cell(0).size() != 1)
460 // fall trough into main branch
463 // do we have an exponent like in
464 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
466 extractScript(exp, jt, ar.end());
468 // create a proper inset as replacement
469 auto_ptr<MathExFuncInset> p(new MathExFuncInset(name));
471 // jt points to the "argument". Get hold of this.
472 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
474 // replace the function name by a real function inset
475 *it = MathAtom(p.release());
477 // remove the source of the argument from the array
478 ar.erase(it + 1, st);
480 // re-insert exponent
481 ar.insert(i + 1, exp);
482 //lyxerr << "\nFunctions to: " << ar << endl;
491 bool testSymbol(MathAtom const & at, string const & name)
493 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
497 bool testIntSymbol(MathAtom const & at)
499 return testSymbol(at, "int");
503 bool testIntegral(MathAtom const & at)
507 ( at->asScriptInset()
508 && at->asScriptInset()->nuc().size()
509 && testIntSymbol(at->asScriptInset()->nuc().back()) );
514 bool testIntDiff(MathAtom const & at)
516 return testString(at, "d");
520 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
521 // assume 'extractDelims' ran before
522 void extractIntegrals(MathArray & ar)
524 // we need at least three items...
528 //lyxerr << "\nIntegrals from: " << ar << endl;
529 for (size_t i = 0; i + 1 < ar.size(); ++i) {
530 MathArray::iterator it = ar.begin() + i;
533 MathArray::iterator jt =
534 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
536 // something sensible found?
540 // is this a integral name?
541 if (!testIntegral(*it))
544 // core ist part from behind the scripts to the 'd'
545 auto_ptr<MathExIntInset> p(new MathExIntInset("int"));
547 // handle scripts if available
548 if (!testIntSymbol(*it)) {
549 p->cell(2) = (*it)->asScriptInset()->down();
550 p->cell(3) = (*it)->asScriptInset()->up();
552 p->cell(0) = MathArray(it + 1, jt);
554 // use the "thing" behind the 'd' as differential
555 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
558 ar.erase(it + 1, tt);
559 *it = MathAtom(p.release());
561 //lyxerr << "\nIntegrals to: " << ar << endl;
570 bool testEqualSign(MathAtom const & at)
572 return testString(at, "=");
576 bool testSumSymbol(MathAtom const & p)
578 return testSymbol(p, "sum");
582 bool testSum(MathAtom const & at)
586 ( at->asScriptInset()
587 && at->asScriptInset()->nuc().size()
588 && testSumSymbol(at->asScriptInset()->nuc().back()) );
592 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
593 // assume 'extractDelims' ran before
594 void extractSums(MathArray & ar)
596 // we need at least two items...
600 //lyxerr << "\nSums from: " << ar << endl;
601 for (size_t i = 0; i + 1 < ar.size(); ++i) {
602 MathArray::iterator it = ar.begin() + i;
604 // is this a sum name?
608 // create a proper inset as replacement
609 auto_ptr<MathExIntInset> p(new MathExIntInset("sum"));
611 // collect lower bound and summation index
612 MathScriptInset const * sub = ar[i]->asScriptInset();
613 if (sub && sub->hasDown()) {
614 // try to figure out the summation index from the subscript
615 MathArray const & ar = sub->down();
616 MathArray::const_iterator xt =
617 find_if(ar.begin(), ar.end(), &testEqualSign);
618 if (xt != ar.end()) {
619 // we found a '=', use everything in front of that as index,
620 // and everything behind as lower index
621 p->cell(1) = MathArray(ar.begin(), xt);
622 p->cell(2) = MathArray(xt + 1, ar.end());
624 // use everything as summation index, don't use scripts.
629 // collect upper bound
630 if (sub && sub->hasUp())
631 p->cell(3) = sub->up();
633 // use something behind the script as core
634 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
637 ar.erase(it + 1, tt);
638 *it = MathAtom(p.release());
640 //lyxerr << "\nSums to: " << ar << endl;
645 // search differential stuff
648 // tests for 'd' or '\partial'
649 bool testDiffItem(MathAtom const & at)
651 return testString(at, "d");
655 bool testDiffArray(MathArray const & ar)
657 return ar.size() && testDiffItem(ar.front());
661 bool testDiffFrac(MathAtom const & at)
665 && testDiffArray(at->asFracInset()->cell(0))
666 && testDiffArray(at->asFracInset()->cell(1));
670 void extractDiff(MathArray & ar)
672 //lyxerr << "\nDiffs from: " << ar << endl;
673 for (size_t i = 0; i < ar.size(); ++i) {
674 MathArray::iterator it = ar.begin() + i;
676 // is this a "differential fraction"?
677 if (!testDiffFrac(*it))
680 MathFracInset const * f = (*it)->asFracInset();
682 lyxerr << "should not happen" << endl;
686 // create a proper diff inset
687 auto_ptr<MathDiffInset> diff(new MathDiffInset);
689 // collect function, let jt point behind last used item
690 MathArray::iterator jt = it + 1;
692 MathArray const & numer = f->cell(0);
693 if (numer.size() > 1 && numer[1]->asScriptInset()) {
694 // this is something like d^n f(x) / d... or d^n / d...
697 if (numer.size() > 2)
698 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
700 jt = extractArgument(diff->cell(0), jt, ar.end());
702 // simply d f(x) / d... or d/d...
703 if (numer.size() > 1)
704 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
706 jt = extractArgument(diff->cell(0), jt, ar.end());
709 // collect denominator parts
710 MathArray const & denom = f->cell(1);
711 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
713 MathArray::const_iterator et
714 = find_if(dt + 1, denom.end(), &testDiffItem);
717 MathArray::const_iterator st = et - 1;
718 MathScriptInset const * script = (*st)->asScriptInset();
719 if (script && script->hasUp()) {
720 // things like d.../dx^n
722 if (extractNumber(script->up(), mult)) {
723 //lyxerr << "mult: " << mult << endl;
724 for (int i = 0; i < mult; ++i)
725 diff->addDer(MathArray(dt + 1, st));
729 diff->addDer(MathArray(dt + 1, et));
735 ar.erase(it + 1, jt);
736 *it = MathAtom(diff.release());
738 //lyxerr << "\nDiffs to: " << ar << endl;
747 bool testRightArrow(MathAtom const & at)
749 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
754 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
755 // assume 'extractDelims' ran before
756 void extractLims(MathArray & ar)
758 // we need at least three items...
762 //lyxerr << "\nLimits from: " << ar << endl;
763 for (size_t i = 0; i + 2 < ar.size(); ++i) {
764 MathArray::iterator it = ar.begin() + i;
766 // is this a limit function?
767 if (!testSymbol(*it, "lim"))
770 // the next one must be a subscript (without superscript)
771 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
772 if (!sub || !sub->hasDown() || sub->hasUp())
775 // and it must contain a -> symbol
776 MathArray const & s = sub->down();
777 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
781 // the -> splits the subscript int x and x0
782 MathArray x = MathArray(s.begin(), st);
783 MathArray x0 = MathArray(st + 1, s.end());
785 // use something behind the script as core
787 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
790 ar.erase(it + 1, tt);
792 // create a proper inset as replacement
793 *it = MathAtom(new MathLimInset(f, x, x0));
795 //lyxerr << "\nLimits to: " << ar << endl;
803 void extractStructure(MathArray & ar)
805 //lyxerr << "\nStructure from: " << ar << endl;
806 extractIntegrals(ar);
812 extractFunctions(ar);
818 //lyxerr << "\nStructure to: " << ar << endl;
822 void write(MathArray const & dat, WriteStream & wi)
826 wi.firstitem() = true;
827 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
829 wi.firstitem() = false;
834 void normalize(MathArray const & ar, NormalStream & os)
836 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
837 (*it)->normalize(os);
841 void octave(MathArray const & dat, OctaveStream & os)
844 extractStructure(ar);
845 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
850 void maple(MathArray const & dat, MapleStream & os)
853 extractStructure(ar);
854 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
859 void maxima(MathArray const & dat, MaximaStream & os)
862 extractStructure(ar);
863 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
868 void mathematica(MathArray const & dat, MathematicaStream & os)
871 extractStructure(ar);
872 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
873 (*it)->mathematica(os);
877 void mathmlize(MathArray const & dat, MathMLStream & os)
880 extractStructure(ar);
883 else if (ar.size() == 1)
887 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
888 (*it)->mathmlize(os);
898 string captureOutput(string const & cmd, string const & data)
900 string command = "echo '" + data + "' | " + cmd;
901 lyxerr << "calling: " << command << endl;
902 cmd_ret const ret = runCommand(command);
906 string::size_type get_matching_brace(string const & str, string::size_type i)
909 string::size_type n = str.size();
911 i = str.find_first_of("{}", i+1);
912 if (i == string::npos) return i;
923 string::size_type get_matching_brace_back(string const & str, string::size_type i)
927 i = str.find_last_of("{}", i-1);
928 if (i == string::npos) return i;
939 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
944 string expr = os.str();
945 string const header = "SIMPSUM:true;";
948 for (int i = 0; i < 100; ++i) { // at most 100 attempts
949 // try to fix missing '*' the hard way
951 // > echo "2x;" | maxima
953 // (C1) Incorrect syntax: x is not an infix operator
957 lyxerr << "checking expr: '" << expr << "'" << endl;
958 string full = header + "tex(" + expr + ");";
959 out = captureOutput("maxima", full);
961 // leave loop if expression syntax is probably ok
962 if (out.find("Incorrect syntax") == string::npos)
965 // search line with "Incorrect syntax"
966 istringstream is(out);
970 if (line.find("Incorrect syntax") != string::npos)
974 // 2nd next line is the one with caret
977 string::size_type pos = line.find('^');
978 lyxerr << "found caret at pos: '" << pos << "'" << endl;
979 if (pos == string::npos || pos < 4)
980 break; // caret position not found
981 pos -= 4; // skip the "tex(" part
982 if (expr[pos] == '*')
983 break; // two '*' in a row are definitely bad
984 expr.insert(pos, "*");
987 vector<string> tmp = getVectorFromString(out, "$$");
991 out = subst(tmp[1],"\\>", "");
992 lyxerr << "out: '" << out << "'" << endl;
994 // Ugly code that tries to make the result prettier
996 string::size_type i = out.find("\\mathchoice");
997 while (i != string::npos) {
998 string::size_type j = get_matching_brace(out, i + 12);
999 string::size_type k = get_matching_brace(out, j + 1);
1000 k = get_matching_brace(out, k + 1);
1001 k = get_matching_brace(out, k + 1);
1002 string mid = out.substr(i + 13,j - i - 13);
1003 if (mid.find("\\over") != string::npos)
1004 mid = '{' + mid + '}';
1005 out = out.substr(0,i)
1007 + out.substr(k + 1);
1008 //lyxerr << "out: " << out << endl;
1009 i = out.find("\\mathchoice", i);
1013 i = out.find("\\over");
1014 while (i != string::npos) {
1015 string::size_type j = get_matching_brace_back(out, i - 1);
1016 if (j == string::npos || j == 0) break;
1017 string::size_type k = get_matching_brace(out, i + 5);
1018 if (k == string::npos || k + 1 == out.size()) break;
1019 out = out.substr(0,j - 1)
1021 + out.substr(j,i - j)
1022 + out.substr(i + 5,k - i - 4)
1023 + out.substr(k + 2);
1024 //lyxerr << "out: " << out << endl;
1025 i = out.find("\\over", i + 4);
1028 mathed_parse_cell(res, out);
1033 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1035 string header = "readlib(latex):\n";
1037 // remove the \\it for variable names
1038 //"#`latex/csname_font` := `\\it `:"
1040 "`latex/csname_font` := ``:\n";
1042 // export matrices in (...) instead of [...]
1044 "`latex/latex/matrix` := "
1045 "subs(`[`=`(`, `]`=`)`,"
1046 "eval(`latex/latex/matrix`)):\n";
1048 // replace \\cdots with proper '*'
1050 "`latex/latex/*` := "
1051 "subs(`\\,`=`\\cdot `,"
1052 "eval(`latex/latex/*`)):\n";
1054 // remove spurious \\noalign{\\medskip} in matrix output
1056 "`latex/latex/matrix`:= "
1057 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1058 "eval(`latex/latex/matrix`)):\n";
1060 //"#`latex/latex/symbol` "
1061 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1063 string trailer = "quit;";
1067 string expr = os.str();
1068 lyxerr << "ar: '" << ar << "'\n"
1069 << "ms: '" << os.str() << "'" << endl;
1071 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1072 // try to fix missing '*' the hard way by using mint
1074 // ... > echo "1A;" | mint -i 1 -S -s -q
1077 // Probably missing an operator such as * p
1079 lyxerr << "checking expr: '" << expr << "'" << endl;
1080 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1082 break; // expression syntax is ok
1083 istringstream is(out);
1086 if (line.find("on line") != 0)
1087 break; // error message not identified
1089 string::size_type pos = line.find('^');
1090 if (pos == string::npos || pos < 15)
1091 break; // caret position not found
1092 pos -= 15; // skip the "on line ..." part
1093 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1094 break; // two '*' in a row are definitely bad
1095 expr.insert(pos, 1, '*');
1098 string full = "latex(" + extra + '(' + expr + "));";
1099 string out = captureOutput("maple -q", header + full + trailer);
1105 mathed_parse_cell(res, out);
1110 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1113 OctaveStream vs(os);
1115 string expr = os.str();
1118 lyxerr << "pipe: ar: '" << ar << "'\n"
1119 << "pipe: expr: '" << expr << "'" << endl;
1121 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1123 // try to fix missing '*' the hard way
1125 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1128 lyxerr << "checking expr: '" << expr << "'" << endl;
1129 out = captureOutput("octave -q 2>&1", expr);
1130 lyxerr << "checking out: '" << out << "'" << endl;
1132 // leave loop if expression syntax is probably ok
1133 if (out.find("parse error:") == string::npos)
1136 // search line with single caret
1137 istringstream is(out);
1141 lyxerr << "skipping line: '" << line << "'" << endl;
1142 if (line.find(">>> ") != string::npos)
1146 // found line with error, next line is the one with caret
1148 string::size_type pos = line.find('^');
1149 lyxerr << "caret line: '" << line << "'" << endl;
1150 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1151 if (pos == string::npos || pos < 4)
1152 break; // caret position not found
1153 pos -= 4; // skip the ">>> " part
1154 if (expr[pos] == '*')
1155 break; // two '*' in a row are definitely bad
1156 expr.insert(pos, 1, '*');
1163 out = out.substr(6);
1165 // parse output as matrix or single number
1166 MathAtom at(new MathArrayInset("array", out));
1167 MathArrayInset const * mat = at->asArrayInset();
1169 if (mat->ncols() == 1 && mat->nrows() == 1)
1170 res.append(mat->cell(0));
1172 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1173 res.back().nucleus()->cell(0).push_back(at);
1181 MathArray pipeThroughExtern(string const & lang, string const & extra,
1182 MathArray const & ar)
1184 if (lang == "octave")
1185 return pipeThroughOctave(extra, ar);
1187 if (lang == "maxima")
1188 return pipeThroughMaxima(extra, ar);
1190 if (lang == "maple")
1191 return pipeThroughMaple(extra, ar);
1193 // create normalized expression
1195 NormalStream ns(os);
1196 os << '[' << extra << ' ';
1199 string data = os.str();
1201 // search external script
1202 string file = libFileSearch("mathed", "extern_" + lang);
1204 lyxerr << "converter to '" << lang << "' not found" << endl;
1208 // run external sript
1209 string out = captureOutput(file, data);
1211 mathed_parse_cell(res, out);