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 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;
56 ostream & operator<<(ostream & os, MathArray const & ar)
64 // define a function for tests
65 typedef bool TestItemFunc(MathAtom const &);
67 // define a function for replacing subexpressions
68 typedef MathAtom ReplaceArgumentFunc(const MathArray & ar);
72 // try to extract a super/subscript
73 // modify iterator position to point behind the thing
74 bool extractScript(MathArray & ar,
75 MathArray::iterator & pos, MathArray::iterator last)
77 // nothing to get here
81 // is this a scriptinset?
82 if (!(*pos)->asScriptInset())
85 // it is a scriptinset, use it.
92 // try to extract an "argument" to some function.
93 // returns position behind the argument
94 MathArray::iterator extractArgument(MathArray & ar,
95 MathArray::iterator pos, MathArray::iterator last, string const & = "")
97 // nothing to get here
101 // something deliminited _is_ an argument
102 if ((*pos)->asDelimInset()) {
107 // always take the first thing, no matter what it is
110 // go ahead if possible
115 // if the next item is a subscript, it most certainly belongs to the
117 extractScript(ar, pos, last);
121 // but it might be more than that.
122 // FIXME: not implemented
123 //for (MathArray::iterator it = pos + 1; it != last; ++it) {
124 // // always take the first thing, no matter
126 // ar.push_back(*it);
134 // returns sequence of char with same code starting at it up to end
135 // it might be less, though...
137 (MathArray::const_iterator it, MathArray::const_iterator end)
140 for (; it != end && (*it)->asCharInset(); ++it)
141 s += (*it)->getChar();
146 void extractStrings(MathArray & ar)
148 //lyxerr << "\nStrings from: " << ar << endl;
149 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
150 if (!ar[i]->asCharInset())
152 string s = charSequence(ar.begin() + i, ar.end());
153 ar[i] = MathAtom(new MathStringInset(s));
154 ar.erase(i + 1, i + s.size());
156 //lyxerr << "\nStrings to: " << ar << endl;
160 void extractMatrices(MathArray & ar)
162 //lyxerr << "\nMatrices from: " << ar << endl;
163 // first pass for explicitly delimited stuff
164 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
165 if (!ar[i]->asDelimInset())
167 MathArray const & arr = ar[i]->asDelimInset()->cell(0);
170 if (!arr.front()->asGridInset())
172 ar[i] = MathAtom(new MathMatrixInset(*(arr.front()->asGridInset())));
175 // second pass for AMS "pmatrix" etc
176 for (MathArray::size_type i = 0; i < ar.size(); ++i)
177 if (ar[i]->asAMSArrayInset())
178 ar[i] = MathAtom(new MathMatrixInset(*(ar[i]->asGridInset())));
179 //lyxerr << "\nMatrices to: " << ar << endl;
183 // convert this inset somehow to a string
184 bool extractString(MathAtom const & at, string & str)
187 str = string(1, at->getChar());
190 if (at->asStringInset()) {
191 str = at->asStringInset()->str();
198 // convert this inset somehow to a number
199 bool extractNumber(MathArray const & ar, int & i)
201 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
207 bool extractNumber(MathArray const & ar, double & d)
209 istringstream is(charSequence(ar.begin(), ar.end()).c_str());
215 bool testString(MathAtom const & at, string const & str)
218 return extractString(at, s) && str == s;
222 // search end of nested sequence
223 MathArray::iterator endNestSearch(
224 MathArray::iterator it,
225 MathArray::iterator last,
226 TestItemFunc testOpen,
227 TestItemFunc testClose
230 for (int level = 0; it != last; ++it) {
242 // replace nested sequences by a real Insets
245 TestItemFunc testOpen,
246 TestItemFunc testClose,
247 ReplaceArgumentFunc replaceArg
250 // use indices rather than iterators for the loop because we are going
251 // to modify the array.
252 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
253 // check whether this is the begin of the sequence
254 if (!testOpen(ar[i]))
257 // search end of sequence
258 MathArray::iterator it = ar.begin() + i;
259 MathArray::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
263 // replace the original stuff by the new inset
264 ar[i] = replaceArg(MathArray(it + 1, jt));
265 ar.erase(it + 1, jt + 1);
272 // split scripts into seperate super- and subscript insets. sub goes in
276 void splitScripts(MathArray & ar)
278 //lyxerr << "\nScripts from: " << ar << endl;
279 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
280 // is this script inset?
281 if (!ar[i]->asScriptInset())
284 // no problem if we don't have both...
285 if (!ar[i]->asScriptInset()->hasUp())
287 if (!ar[i]->asScriptInset()->hasDown())
290 // create extra script inset and move superscript over
291 MathScriptInset * p = ar[i].nucleus()->asScriptInset();
292 MathScriptInset * q = new MathScriptInset(true);
293 std::swap(q->up(), p->up());
294 p->removeScript(true);
296 // insert new inset behind
298 ar.insert(i, MathAtom(q));
300 //lyxerr << "\nScripts to: " << ar << endl;
308 void extractExps(MathArray & ar)
310 //lyxerr << "\nExps from: " << ar << endl;
311 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
313 if (ar[i]->getChar() != 'e')
316 // we need an exponent but no subscript
317 MathScriptInset const * sup = ar[i + 1]->asScriptInset();
318 if (!sup || sup->hasDown())
321 // create a proper exp-inset as replacement
322 ar[i] = MathAtom(new MathExFuncInset("exp", sup->cell(1)));
325 //lyxerr << "\nExps to: " << ar << endl;
330 // extract det(...) from |matrix|
332 void extractDets(MathArray & ar)
334 //lyxerr << "\ndet from: " << ar << endl;
335 for (MathArray::iterator it = ar.begin(); it != ar.end(); ++it) {
336 MathDelimInset const * del = (*it)->asDelimInset();
341 *it = MathAtom(new MathExFuncInset("det", del->cell(0)));
343 //lyxerr << "\ndet to: " << ar << endl;
351 bool isDigitOrSimilar(char c)
353 return ('0' <= c && c <= '9') || c == '.';
357 // returns sequence of digits
359 (MathArray::const_iterator it, MathArray::const_iterator end)
362 for (; it != end && (*it)->asCharInset(); ++it) {
363 if (!isDigitOrSimilar((*it)->getChar()))
365 s += (*it)->getChar();
371 void extractNumbers(MathArray & ar)
373 //lyxerr << "\nNumbers from: " << ar << endl;
374 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
375 if (!ar[i]->asCharInset())
377 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
380 string s = digitSequence(ar.begin() + i, ar.end());
382 ar[i] = MathAtom(new MathNumberInset(s));
383 ar.erase(i + 1, i + s.size());
385 //lyxerr << "\nNumbers to: " << ar << endl;
391 // search deliminiters
394 bool testOpenParan(MathAtom const & at)
396 return testString(at, "(");
400 bool testCloseParan(MathAtom const & at)
402 return testString(at, ")");
406 MathAtom replaceDelims(const MathArray & ar)
408 return MathAtom(new MathDelimInset("(", ")", ar));
412 // replace '('...')' sequences by a real MathDelimInset
413 void extractDelims(MathArray & ar)
415 //lyxerr << "\nDelims from: " << ar << endl;
416 replaceNested(ar, testOpenParan, testCloseParan, replaceDelims);
417 //lyxerr << "\nDelims to: " << ar << endl;
423 // search well-known functions
427 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real MathExFuncInset
428 // assume 'extractDelims' ran before
429 void extractFunctions(MathArray & ar)
431 // we need at least two items...
435 //lyxerr << "\nFunctions from: " << ar << endl;
436 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
437 MathArray::iterator it = ar.begin() + i;
438 MathArray::iterator jt = it + 1;
442 if ((*it)->asUnknownInset()) {
443 // it certainly is if it is well known...
444 name = (*it)->name();
446 // is this a user defined function?
447 // it it probably not, if it doesn't have a name.
448 if (!extractString(*it, name))
450 // it is not if it has no argument
453 // guess so, if this is followed by
454 // a DelimInset with a single item in the cell
455 MathDelimInset const * del = (*jt)->asDelimInset();
456 if (!del || del->cell(0).size() != 1)
458 // fall trough into main branch
461 // do we have an exponent like in
462 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
464 extractScript(exp, jt, ar.end());
466 // create a proper inset as replacement
467 MathExFuncInset * p = new MathExFuncInset(name);
469 // jt points to the "argument". Get hold of this.
470 MathArray::iterator st = extractArgument(p->cell(0), jt, ar.end());
472 // replace the function name by a real function inset
475 // remove the source of the argument from the array
476 ar.erase(it + 1, st);
478 // re-insert exponent
479 ar.insert(i + 1, exp);
480 //lyxerr << "\nFunctions to: " << ar << endl;
489 bool testSymbol(MathAtom const & at, string const & name)
491 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
495 bool testIntSymbol(MathAtom const & at)
497 return testSymbol(at, "int");
501 bool testIntegral(MathAtom const & at)
505 ( at->asScriptInset()
506 && at->asScriptInset()->nuc().size()
507 && testIntSymbol(at->asScriptInset()->nuc().back()) );
512 bool testIntDiff(MathAtom const & at)
514 return testString(at, "d");
518 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real MathExIntInset
519 // assume 'extractDelims' ran before
520 void extractIntegrals(MathArray & ar)
522 // we need at least three items...
526 //lyxerr << "\nIntegrals from: " << ar << endl;
527 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
528 MathArray::iterator it = ar.begin() + i;
531 MathArray::iterator jt =
532 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
534 // something sensible found?
538 // is this a integral name?
539 if (!testIntegral(*it))
542 // core ist part from behind the scripts to the 'd'
543 MathExIntInset * p = new MathExIntInset("int");
545 // handle scripts if available
546 if (!testIntSymbol(*it)) {
547 p->cell(2) = (*it)->asScriptInset()->down();
548 p->cell(3) = (*it)->asScriptInset()->up();
550 p->cell(0) = MathArray(it + 1, jt);
552 // use the "thing" behind the 'd' as differential
553 MathArray::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
556 ar.erase(it + 1, tt);
559 //lyxerr << "\nIntegrals to: " << ar << endl;
568 bool testEqualSign(MathAtom const & at)
570 return testString(at, "=");
574 bool testSumSymbol(MathAtom const & p)
576 return testSymbol(p, "sum");
580 bool testSum(MathAtom const & at)
584 ( at->asScriptInset()
585 && at->asScriptInset()->nuc().size()
586 && testSumSymbol(at->asScriptInset()->nuc().back()) );
590 // replace '\sum' ['_^'] f(x) sequences by a real MathExIntInset
591 // assume 'extractDelims' ran before
592 void extractSums(MathArray & ar)
594 // we need at least two items...
598 //lyxerr << "\nSums from: " << ar << endl;
599 for (MathArray::size_type i = 0; i + 1 < ar.size(); ++i) {
600 MathArray::iterator it = ar.begin() + i;
602 // is this a sum name?
606 // create a proper inset as replacement
607 MathExIntInset * p = new MathExIntInset("sum");
609 // collect lower bound and summation index
610 MathScriptInset const * sub = ar[i]->asScriptInset();
611 if (sub && sub->hasDown()) {
612 // try to figure out the summation index from the subscript
613 MathArray const & ar = sub->down();
614 MathArray::const_iterator xt =
615 find_if(ar.begin(), ar.end(), &testEqualSign);
616 if (xt != ar.end()) {
617 // we found a '=', use everything in front of that as index,
618 // and everything behind as lower index
619 p->cell(1) = MathArray(ar.begin(), xt);
620 p->cell(2) = MathArray(xt + 1, ar.end());
622 // use everything as summation index, don't use scripts.
627 // collect upper bound
628 if (sub && sub->hasUp())
629 p->cell(3) = sub->up();
631 // use something behind the script as core
632 MathArray::iterator tt = extractArgument(p->cell(0), it + 1, ar.end());
635 ar.erase(it + 1, tt);
638 //lyxerr << "\nSums to: " << ar << endl;
643 // search differential stuff
646 // tests for 'd' or '\partial'
647 bool testDiffItem(MathAtom const & at)
649 return testString(at, "d");
653 bool testDiffArray(MathArray const & ar)
655 return ar.size() && testDiffItem(ar.front());
659 bool testDiffFrac(MathAtom const & at)
663 && testDiffArray(at->asFracInset()->cell(0))
664 && testDiffArray(at->asFracInset()->cell(1));
668 void extractDiff(MathArray & ar)
670 //lyxerr << "\nDiffs from: " << ar << endl;
671 for (MathArray::size_type i = 0; i < ar.size(); ++i) {
672 MathArray::iterator it = ar.begin() + i;
674 // is this a "differential fraction"?
675 if (!testDiffFrac(*it))
678 MathFracInset const * f = (*it)->asFracInset();
680 lyxerr << "should not happen" << endl;
684 // create a proper diff inset
685 MathDiffInset * diff = new MathDiffInset;
687 // collect function, let jt point behind last used item
688 MathArray::iterator jt = it + 1;
690 MathArray const & numer = f->cell(0);
691 if (numer.size() > 1 && numer[1]->asScriptInset()) {
692 // this is something like d^n f(x) / d... or d^n / d...
695 if (numer.size() > 2)
696 diff->cell(0) = MathArray(numer.begin() + 2, numer.end());
698 jt = extractArgument(diff->cell(0), jt, ar.end());
700 // simply d f(x) / d... or d/d...
701 if (numer.size() > 1)
702 diff->cell(0) = MathArray(numer.begin() + 1, numer.end());
704 jt = extractArgument(diff->cell(0), jt, ar.end());
707 // collect denominator parts
708 MathArray const & denom = f->cell(1);
709 for (MathArray::const_iterator dt = denom.begin(); dt != denom.end();) {
711 MathArray::const_iterator et
712 = find_if(dt + 1, denom.end(), &testDiffItem);
715 MathArray::const_iterator st = et - 1;
716 MathScriptInset const * script = (*st)->asScriptInset();
717 if (script && script->hasUp()) {
718 // things like d.../dx^n
720 if (extractNumber(script->up(), mult)) {
721 //lyxerr << "mult: " << mult << endl;
722 for (int i = 0; i < mult; ++i)
723 diff->addDer(MathArray(dt + 1, st));
727 diff->addDer(MathArray(dt + 1, et));
733 ar.erase(it + 1, jt);
734 *it = MathAtom(diff);
736 //lyxerr << "\nDiffs to: " << ar << endl;
745 bool testRightArrow(MathAtom const & at)
747 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
752 // replace '\lim_{x->x0} f(x)' sequences by a real MathLimInset
753 // assume 'extractDelims' ran before
754 void extractLims(MathArray & ar)
756 // we need at least three items...
760 //lyxerr << "\nLimits from: " << ar << endl;
761 for (MathArray::size_type i = 0; i + 2 < ar.size(); ++i) {
762 MathArray::iterator it = ar.begin() + i;
764 // is this a limit function?
765 if (!testSymbol(*it, "lim"))
768 // the next one must be a subscript (without superscript)
769 MathScriptInset const * sub = (*(it + 1))->asScriptInset();
770 if (!sub || !sub->hasDown() || sub->hasUp())
773 // and it must contain a -> symbol
774 MathArray const & s = sub->down();
775 MathArray::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
779 // the -> splits the subscript int x and x0
780 MathArray x = MathArray(s.begin(), st);
781 MathArray x0 = MathArray(st + 1, s.end());
783 // use something behind the script as core
785 MathArray::iterator tt = extractArgument(f, it + 2, ar.end());
788 ar.erase(it + 1, tt);
790 // create a proper inset as replacement
791 *it = MathAtom(new MathLimInset(f, x, x0));
793 //lyxerr << "\nLimits to: " << ar << endl;
801 void extractStructure(MathArray & ar)
803 //lyxerr << "\nStructure from: " << ar << endl;
804 extractIntegrals(ar);
810 extractFunctions(ar);
816 //lyxerr << "\nStructure to: " << ar << endl;
820 void write(MathArray const & dat, WriteStream & wi)
824 wi.firstitem() = true;
825 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it) {
827 wi.firstitem() = false;
832 void normalize(MathArray const & ar, NormalStream & os)
834 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
835 (*it)->normalize(os);
839 void octave(MathArray const & dat, OctaveStream & os)
842 extractStructure(ar);
843 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
848 void maple(MathArray const & dat, MapleStream & os)
851 extractStructure(ar);
852 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
857 void maxima(MathArray const & dat, MaximaStream & os)
860 extractStructure(ar);
861 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
866 void mathematica(MathArray const & dat, MathematicaStream & os)
869 extractStructure(ar);
870 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
871 (*it)->mathematica(os);
875 void mathmlize(MathArray const & dat, MathMLStream & os)
878 extractStructure(ar);
881 else if (ar.size() == 1)
885 for (MathArray::const_iterator it = ar.begin(); it != ar.end(); ++it)
886 (*it)->mathmlize(os);
896 string captureOutput(string const & cmd, string const & data)
898 string command = "echo '" + data + "' | " + cmd;
899 lyxerr << "calling: " << command << endl;
900 cmd_ret const ret = RunCommand(command);
904 string::size_type get_matching_brace(string const & str, string::size_type i)
907 string::size_type n = str.size();
909 i = str.find_first_of("{}", i+1);
910 if (i == string::npos) return i;
921 string::size_type get_matching_brace_back(string const & str, string::size_type i)
925 i = str.find_last_of("{}", i-1);
926 if (i == string::npos) return i;
937 MathArray pipeThroughMaxima(string const &, MathArray const & ar)
942 string expr = os.str();
943 string const header = "SIMPSUM:true;";
946 for (int i = 0; i < 100; ++i) { // at most 100 attempts
947 // try to fix missing '*' the hard way
949 // > echo "2x;" | maxima
951 // (C1) Incorrect syntax: x is not an infix operator
955 lyxerr << "checking expr: '" << expr << "'" << endl;
956 string full = header + "tex(" + expr + ");";
957 out = captureOutput("maxima", full);
959 // leave loop if expression syntax is probably ok
960 if (out.find("Incorrect syntax") == string::npos)
963 // search line with "Incorrect syntax"
964 istringstream is(out.c_str());
968 if (line.find("Incorrect syntax") != string::npos)
972 // 2nd next line is the one with caret
975 string::size_type pos = line.find('^');
976 lyxerr << "found caret at pos: '" << pos << "'" << endl;
977 if (pos == string::npos || pos < 4)
978 break; // caret position not found
979 pos -= 4; // skip the "tex(" part
980 if (expr[pos] == '*')
981 break; // two '*' in a row are definitely bad
982 expr.insert(pos, "*");
985 std::vector<string> tmp = getVectorFromString(out, "$$");
989 out = subst(tmp[1],"\\>", "");
990 lyxerr << "out: '" << out << "'" << endl;
992 // Ugly code that tries to make the result prettier
994 string::size_type i = out.find("\\mathchoice");
995 while (i != string::npos) {
996 string::size_type j = get_matching_brace(out, i + 12);
997 string::size_type k = get_matching_brace(out, j + 1);
998 k = get_matching_brace(out, k + 1);
999 k = get_matching_brace(out, k + 1);
1000 string mid = out.substr(i + 13,j - i - 13);
1001 if (mid.find("\\over") != string::npos)
1002 mid = '{' + mid + '}';
1003 out = out.substr(0,i)
1005 + out.substr(k + 1);
1006 //lyxerr << "out: " << out << endl;
1007 i = out.find("\\mathchoice", i);
1011 i = out.find("\\over");
1012 while (i != string::npos) {
1013 string::size_type j = get_matching_brace_back(out, i - 1);
1014 if (j == string::npos || j == 0) break;
1015 string::size_type k = get_matching_brace(out, i + 5);
1016 if (k == string::npos || k + 1 == out.size()) break;
1017 out = out.substr(0,j - 1)
1019 + out.substr(j,i - j)
1020 + out.substr(i + 5,k - i - 4)
1021 + out.substr(k + 2);
1022 //lyxerr << "out: " << out << endl;
1023 i = out.find("\\over", i + 4);
1026 mathed_parse_cell(res, out);
1031 MathArray pipeThroughMaple(string const & extra, MathArray const & ar)
1033 string header = "readlib(latex):\n";
1035 // remove the \\it for variable names
1036 //"#`latex/csname_font` := `\\it `:"
1038 "`latex/csname_font` := ``:\n";
1040 // export matrices in (...) instead of [...]
1042 "`latex/latex/matrix` := "
1043 "subs(`[`=`(`, `]`=`)`,"
1044 "eval(`latex/latex/matrix`)):\n";
1046 // replace \\cdots with proper '*'
1048 "`latex/latex/*` := "
1049 "subs(`\\,`=`\\cdot `,"
1050 "eval(`latex/latex/*`)):\n";
1052 // remove spurious \\noalign{\\medskip} in matrix output
1054 "`latex/latex/matrix`:= "
1055 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1056 "eval(`latex/latex/matrix`)):\n";
1058 //"#`latex/latex/symbol` "
1059 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1061 string trailer = "quit;";
1065 string expr = os.str();
1066 lyxerr << "ar: '" << ar << "'\n"
1067 << "ms: '" << os.str() << "'" << endl;
1069 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1070 // try to fix missing '*' the hard way by using mint
1072 // ... > echo "1A;" | mint -i 1 -S -s -q
1075 // Probably missing an operator such as * p
1077 lyxerr << "checking expr: '" << expr << "'" << endl;
1078 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1080 break; // expression syntax is ok
1081 istringstream is(out.c_str());
1084 if (line.find("on line") != 0)
1085 break; // error message not identified
1087 string::size_type pos = line.find('^');
1088 if (pos == string::npos || pos < 15)
1089 break; // caret position not found
1090 pos -= 15; // skip the "on line ..." part
1091 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1092 break; // two '*' in a row are definitely bad
1093 expr.insert(pos, 1, '*');
1096 string full = "latex(" + extra + '(' + expr + "));";
1097 string out = captureOutput("maple -q", header + full + trailer);
1103 mathed_parse_cell(res, out);
1108 MathArray pipeThroughOctave(string const &, MathArray const & ar)
1111 OctaveStream vs(os);
1113 string expr = os.str();
1116 lyxerr << "pipe: ar: '" << ar << "'\n"
1117 << "pipe: expr: '" << expr << "'" << endl;
1119 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1121 // try to fix missing '*' the hard way
1123 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1126 lyxerr << "checking expr: '" << expr << "'" << endl;
1127 out = captureOutput("octave -q 2>&1", expr);
1128 lyxerr << "checking out: '" << out << "'" << endl;
1130 // leave loop if expression syntax is probably ok
1131 if (out.find("parse error:") == string::npos)
1134 // search line with single caret
1135 istringstream is(out.c_str());
1139 lyxerr << "skipping line: '" << line << "'" << endl;
1140 if (line.find(">>> ") != string::npos)
1144 // found line with error, next line is the one with caret
1146 string::size_type pos = line.find('^');
1147 lyxerr << "caret line: '" << line << "'" << endl;
1148 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1149 if (pos == string::npos || pos < 4)
1150 break; // caret position not found
1151 pos -= 4; // skip the ">>> " part
1152 if (expr[pos] == '*')
1153 break; // two '*' in a row are definitely bad
1154 expr.insert(pos, 1, '*');
1161 out = out.substr(6);
1163 // parse output as matrix or single number
1164 MathAtom at(new MathArrayInset("array", out));
1165 MathArrayInset const * mat = at->asArrayInset();
1167 if (mat->ncols() == 1 && mat->nrows() == 1)
1168 res.append(mat->cell(0));
1170 res.push_back(MathAtom(new MathDelimInset("(", ")")));
1171 res.back().nucleus()->cell(0).push_back(at);
1179 MathArray pipeThroughExtern(string const & lang, string const & extra,
1180 MathArray const & ar)
1182 if (lang == "octave")
1183 return pipeThroughOctave(extra, ar);
1185 if (lang == "maxima")
1186 return pipeThroughMaxima(extra, ar);
1188 if (lang == "maple")
1189 return pipeThroughMaple(extra, ar);
1191 // create normalized expression
1193 NormalStream ns(os);
1194 os << '[' << extra << ' ';
1197 string data = os.str();
1199 // search external script
1200 string file = LibFileSearch("mathed", "extern_" + lang);
1202 lyxerr << "converter to '" << lang << "' not found" << endl;
1206 // run external sript
1207 string out = captureOutput(file, data);
1209 mathed_parse_cell(res, out);