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
17 #include "MathExtern.h"
20 #include "InsetMathArray.h"
21 #include "InsetMathChar.h"
22 #include "InsetMathDelim.h"
23 #include "InsetMathDiff.h"
24 #include "InsetMathExFunc.h"
25 #include "InsetMathExInt.h"
26 #include "InsetMathFont.h"
27 #include "InsetMathFrac.h"
28 #include "InsetMathLim.h"
29 #include "InsetMathMatrix.h"
30 #include "InsetMathNumber.h"
31 #include "InsetMathScript.h"
32 #include "InsetMathString.h"
33 #include "InsetMathSymbol.h"
35 #include "MathParser.h"
36 #include "MathStream.h"
38 #include "support/docstream.h"
39 #include "support/filetools.h"
40 #include "support/lstrings.h"
41 #include "support/lyxlib.h"
50 using support::cmd_ret;
51 using support::getVectorFromString;
52 using support::libFileSearch;
53 using support::runCommand;
54 using support::FileName;
55 using support::quoteName;
56 using support::tempName;
62 using std::istringstream;
68 static char const * function_names[] = {
69 "arccos", "arcsin", "arctan", "arg", "bmod",
70 "cos", "cosh", "cot", "coth", "csc", "deg",
71 "det", "dim", "exp", "gcd", "hom", "inf", "ker",
72 "lg", "lim", "liminf", "limsup", "ln", "log",
73 "max", "min", "sec", "sin", "sinh", "sup",
74 "tan", "tanh", "Pr", 0
77 static size_t const npos = lyx::docstring::npos;
79 // define a function for tests
80 typedef bool TestItemFunc(MathAtom const &);
82 // define a function for replacing subexpressions
83 typedef MathAtom ReplaceArgumentFunc(const MathData & ar);
87 // try to extract a super/subscript
88 // modify iterator position to point behind the thing
89 bool extractScript(MathData & ar,
90 MathData::iterator & pos, MathData::iterator last, bool superscript)
92 // nothing to get here
96 // is this a scriptinset?
97 if (!(*pos)->asScriptInset())
100 // do we want superscripts only?
101 if (superscript && !(*pos)->asScriptInset()->hasUp())
104 // it is a scriptinset, use it.
111 // try to extract an "argument" to some function.
112 // returns position behind the argument
113 MathData::iterator extractArgument(MathData & ar,
114 MathData::iterator pos, MathData::iterator last, bool function = false)
116 // nothing to get here
120 // something delimited _is_ an argument
121 if ((*pos)->asDelimInset()) {
122 // leave out delimiters if this is a function argument
124 MathData const & arg = (*pos)->asDelimInset()->cell(0);
125 MathData::const_iterator cur = arg.begin();
126 MathData::const_iterator end = arg.end();
128 ar.push_back(*cur++);
134 // if there's one, get following superscript only if this
135 // isn't a function argument
137 extractScript(ar, pos, last, true);
141 // always take the first thing, no matter what it is
144 // go ahead if possible
149 // if the next item is a super/subscript, it most certainly belongs
150 // to the thing we have
151 extractScript(ar, pos, last, false);
155 // but it might be more than that.
156 // FIXME: not implemented
157 //for (MathData::iterator it = pos + 1; it != last; ++it) {
158 // // always take the first thing, no matter
160 // ar.push_back(*it);
168 // returns sequence of char with same code starting at it up to end
169 // it might be less, though...
170 docstring charSequence
171 (MathData::const_iterator it, MathData::const_iterator end)
174 for (; it != end && (*it)->asCharInset(); ++it)
175 s += (*it)->getChar();
180 void extractStrings(MathData & ar)
182 //lyxerr << "\nStrings from: " << ar << endl;
183 for (size_t i = 0; i < ar.size(); ++i) {
184 if (!ar[i]->asCharInset())
186 docstring s = charSequence(ar.begin() + i, ar.end());
187 ar[i] = MathAtom(new InsetMathString(s));
188 ar.erase(i + 1, i + s.size());
190 //lyxerr << "\nStrings to: " << ar << endl;
194 void extractMatrices(MathData & ar)
196 //lyxerr << "\nMatrices from: " << ar << endl;
197 // first pass for explicitly delimited stuff
198 for (size_t i = 0; i < ar.size(); ++i) {
199 if (!ar[i]->asDelimInset())
201 MathData const & arr = ar[i]->asDelimInset()->cell(0);
204 if (!arr.front()->asGridInset())
206 ar[i] = MathAtom(new InsetMathMatrix(*(arr.front()->asGridInset())));
209 // second pass for AMS "pmatrix" etc
210 for (size_t i = 0; i < ar.size(); ++i)
211 if (ar[i]->asAMSArrayInset())
212 ar[i] = MathAtom(new InsetMathMatrix(*(ar[i]->asGridInset())));
213 //lyxerr << "\nMatrices to: " << ar << endl;
217 // convert this inset somehow to a string
218 bool extractString(MathAtom const & at, docstring & str)
221 str = docstring(1, at->getChar());
224 if (at->asStringInset()) {
225 str = at->asStringInset()->str();
232 // is this a known function?
233 bool isKnownFunction(docstring const & str)
235 for (int i = 0; function_names[i]; ++i) {
236 if (str == function_names[i])
243 // extract a function name from this inset
244 bool extractFunctionName(MathAtom const & at, docstring & str)
246 if (at->asSymbolInset()) {
247 str = at->asSymbolInset()->name();
248 return isKnownFunction(str);
250 if (at->asUnknownInset()) {
251 // assume it is well known...
255 if (at->asFontInset() && at->name() == "mathrm") {
256 // assume it is well known...
257 MathData const & ar = at->asFontInset()->cell(0);
258 str = charSequence(ar.begin(), ar.end());
259 return ar.size() == str.size();
265 // convert this inset somehow to a number
266 bool extractNumber(MathData const & ar, int & i)
268 idocstringstream is(charSequence(ar.begin(), ar.end()));
274 bool extractNumber(MathData const & ar, double & d)
276 idocstringstream is(charSequence(ar.begin(), ar.end()));
282 bool testString(MathAtom const & at, docstring const & str)
285 return extractString(at, s) && str == s;
289 bool testString(MathAtom const & at, char const * const str)
291 return testString(at, from_ascii(str));
294 // search end of nested sequence
295 MathData::iterator endNestSearch(
296 MathData::iterator it,
297 MathData::iterator last,
298 TestItemFunc testOpen,
299 TestItemFunc testClose
302 for (int level = 0; it != last; ++it) {
314 // replace nested sequences by a real Insets
317 TestItemFunc testOpen,
318 TestItemFunc testClose,
319 ReplaceArgumentFunc replaceArg
322 // use indices rather than iterators for the loop because we are going
323 // to modify the array.
324 for (size_t i = 0; i < ar.size(); ++i) {
325 // check whether this is the begin of the sequence
326 if (!testOpen(ar[i]))
329 // search end of sequence
330 MathData::iterator it = ar.begin() + i;
331 MathData::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
335 // replace the original stuff by the new inset
336 ar[i] = replaceArg(MathData(it + 1, jt));
337 ar.erase(it + 1, jt + 1);
344 // split scripts into seperate super- and subscript insets. sub goes in
348 void splitScripts(MathData & ar)
350 //lyxerr << "\nScripts from: " << ar << endl;
351 for (size_t i = 0; i < ar.size(); ++i) {
352 InsetMathScript const * script = ar[i]->asScriptInset();
354 // is this a script inset and do we also have a superscript?
355 if (!script || !script->hasUp())
358 // we must have a nucleus if we only have a superscript
359 if (!script->hasDown() && script->nuc().size() == 0)
362 if (script->nuc().size() == 1) {
363 // leave alone sums and integrals
364 InsetMathSymbol const * sym =
365 script->nuc().front()->asSymbolInset();
366 if (sym && (sym->name() == "sum" || sym->name() == "int"))
370 // create extra script inset and move superscript over
371 InsetMathScript * p = ar[i].nucleus()->asScriptInset();
372 std::auto_ptr<InsetMathScript> q(new InsetMathScript(true));
373 swap(q->up(), p->up());
374 p->removeScript(true);
376 // if we don't have a subscript, get rid of the ScriptInset
377 if (!script->hasDown()) {
378 MathData arg(p->nuc());
379 MathData::const_iterator it = arg.begin();
380 MathData::const_iterator et = arg.end();
383 ar.insert(i++, *it++);
387 // insert new inset behind
388 ar.insert(i, MathAtom(q.release()));
390 //lyxerr << "\nScripts to: " << ar << endl;
398 void extractExps(MathData & ar)
400 //lyxerr << "\nExps from: " << ar << endl;
401 for (size_t i = 0; i + 1 < ar.size(); ++i) {
403 if (ar[i]->getChar() != 'e')
406 // we need an exponent but no subscript
407 InsetMathScript const * sup = ar[i + 1]->asScriptInset();
408 if (!sup || sup->hasDown())
411 // create a proper exp-inset as replacement
412 ar[i] = MathAtom(new InsetMathExFunc(from_ascii("exp"), sup->cell(1)));
415 //lyxerr << "\nExps to: " << ar << endl;
420 // extract det(...) from |matrix|
422 void extractDets(MathData & ar)
424 //lyxerr << "\ndet from: " << ar << endl;
425 for (MathData::iterator it = ar.begin(); it != ar.end(); ++it) {
426 InsetMathDelim const * del = (*it)->asDelimInset();
431 *it = MathAtom(new InsetMathExFunc(from_ascii("det"), del->cell(0)));
433 //lyxerr << "\ndet to: " << ar << endl;
441 bool isDigitOrSimilar(char_type c)
443 return ('0' <= c && c <= '9') || c == '.';
447 // returns sequence of digits
448 docstring digitSequence
449 (MathData::const_iterator it, MathData::const_iterator end)
452 for (; it != end && (*it)->asCharInset(); ++it) {
453 if (!isDigitOrSimilar((*it)->getChar()))
455 s += (*it)->getChar();
461 void extractNumbers(MathData & ar)
463 //lyxerr << "\nNumbers from: " << ar << endl;
464 for (size_t i = 0; i < ar.size(); ++i) {
465 if (!ar[i]->asCharInset())
467 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
470 docstring s = digitSequence(ar.begin() + i, ar.end());
472 ar[i] = MathAtom(new InsetMathNumber(s));
473 ar.erase(i + 1, i + s.size());
475 //lyxerr << "\nNumbers to: " << ar << endl;
484 bool testOpenParen(MathAtom const & at)
486 return testString(at, "(");
490 bool testCloseParen(MathAtom const & at)
492 return testString(at, ")");
496 MathAtom replaceParenDelims(const MathData & ar)
498 return MathAtom(new InsetMathDelim(from_ascii("("), from_ascii(")"), ar));
502 bool testOpenBracket(MathAtom const & at)
504 return testString(at, "[");
508 bool testCloseBracket(MathAtom const & at)
510 return testString(at, "]");
514 MathAtom replaceBracketDelims(const MathData & ar)
516 return MathAtom(new InsetMathDelim(from_ascii("["), from_ascii("]"), ar));
520 // replace '('...')' and '['...']' sequences by a real InsetMathDelim
521 void extractDelims(MathData & ar)
523 //lyxerr << "\nDelims from: " << ar << endl;
524 replaceNested(ar, testOpenParen, testCloseParen, replaceParenDelims);
525 replaceNested(ar, testOpenBracket, testCloseBracket, replaceBracketDelims);
526 //lyxerr << "\nDelims to: " << ar << endl;
532 // search well-known functions
536 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real InsetMathExFunc
537 // assume 'extractDelims' ran before
538 void extractFunctions(MathData & ar)
540 // we need at least two items...
544 //lyxerr << "\nFunctions from: " << ar << endl;
545 for (size_t i = 0; i + 1 < ar.size(); ++i) {
546 MathData::iterator it = ar.begin() + i;
547 MathData::iterator jt = it + 1;
551 // it certainly is if it is well known...
552 if (!extractFunctionName(*it, name)) {
553 // is this a user defined function?
554 // it it probably not, if it doesn't have a name.
555 if (!extractString(*it, name))
557 // it is not if it has no argument
560 // guess so, if this is followed by
561 // a DelimInset with a single item in the cell
562 InsetMathDelim const * del = (*jt)->asDelimInset();
563 if (!del || del->cell(0).size() != 1)
565 // fall trough into main branch
568 // do we have an exponent like in
569 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
571 extractScript(exp, jt, ar.end(), true);
573 // create a proper inset as replacement
574 std::auto_ptr<InsetMathExFunc> p(new InsetMathExFunc(name));
576 // jt points to the "argument". Get hold of this.
577 MathData::iterator st = extractArgument(p->cell(0), jt, ar.end(), true);
579 // replace the function name by a real function inset
580 *it = MathAtom(p.release());
582 // remove the source of the argument from the array
583 ar.erase(it + 1, st);
585 // re-insert exponent
586 ar.insert(i + 1, exp);
587 //lyxerr << "\nFunctions to: " << ar << endl;
596 bool testSymbol(MathAtom const & at, docstring const & name)
598 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
602 bool testSymbol(MathAtom const & at, char const * const name)
604 return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
608 bool testIntSymbol(MathAtom const & at)
610 return testSymbol(at, from_ascii("int"));
614 bool testIntegral(MathAtom const & at)
618 ( at->asScriptInset()
619 && at->asScriptInset()->nuc().size()
620 && testIntSymbol(at->asScriptInset()->nuc().back()) );
625 bool testIntDiff(MathAtom const & at)
627 return testString(at, "d");
631 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
632 // assume 'extractDelims' ran before
633 void extractIntegrals(MathData & ar)
635 // we need at least three items...
639 //lyxerr << "\nIntegrals from: " << ar << endl;
640 for (size_t i = 0; i + 1 < ar.size(); ++i) {
641 MathData::iterator it = ar.begin() + i;
644 MathData::iterator jt =
645 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
647 // something sensible found?
651 // is this a integral name?
652 if (!testIntegral(*it))
655 // core ist part from behind the scripts to the 'd'
656 auto_ptr<InsetMathExInt> p(new InsetMathExInt(from_ascii("int")));
658 // handle scripts if available
659 if (!testIntSymbol(*it)) {
660 p->cell(2) = (*it)->asScriptInset()->down();
661 p->cell(3) = (*it)->asScriptInset()->up();
663 p->cell(0) = MathData(it + 1, jt);
665 // use the "thing" behind the 'd' as differential
666 MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end());
669 ar.erase(it + 1, tt);
670 *it = MathAtom(p.release());
672 //lyxerr << "\nIntegrals to: " << ar << endl;
676 bool testTermDelimiter(MathAtom const & at)
678 return testString(at, "+") || testString(at, "-");
682 // try to extract a "term", i.e., something delimited by '+' or '-'.
683 // returns position behind the term
684 MathData::iterator extractTerm(MathData & ar,
685 MathData::iterator pos, MathData::iterator last)
687 while (pos != last && !testTermDelimiter(*pos)) {
700 bool testEqualSign(MathAtom const & at)
702 return testString(at, "=");
706 bool testSumSymbol(MathAtom const & p)
708 return testSymbol(p, from_ascii("sum"));
712 bool testSum(MathAtom const & at)
716 ( at->asScriptInset()
717 && at->asScriptInset()->nuc().size()
718 && testSumSymbol(at->asScriptInset()->nuc().back()) );
722 // replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
723 // assume 'extractDelims' ran before
724 void extractSums(MathData & ar)
726 // we need at least two items...
730 //lyxerr << "\nSums from: " << ar << endl;
731 for (size_t i = 0; i + 1 < ar.size(); ++i) {
732 MathData::iterator it = ar.begin() + i;
734 // is this a sum name?
738 // create a proper inset as replacement
739 auto_ptr<InsetMathExInt> p(new InsetMathExInt(from_ascii("sum")));
741 // collect lower bound and summation index
742 InsetMathScript const * sub = ar[i]->asScriptInset();
743 if (sub && sub->hasDown()) {
744 // try to figure out the summation index from the subscript
745 MathData const & ar = sub->down();
746 MathData::const_iterator xt =
747 find_if(ar.begin(), ar.end(), &testEqualSign);
748 if (xt != ar.end()) {
749 // we found a '=', use everything in front of that as index,
750 // and everything behind as lower index
751 p->cell(1) = MathData(ar.begin(), xt);
752 p->cell(2) = MathData(xt + 1, ar.end());
754 // use everything as summation index, don't use scripts.
759 // collect upper bound
760 if (sub && sub->hasUp())
761 p->cell(3) = sub->up();
763 // use something behind the script as core
764 MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
767 ar.erase(it + 1, tt);
768 *it = MathAtom(p.release());
770 //lyxerr << "\nSums to: " << ar << endl;
775 // search differential stuff
778 // tests for 'd' or '\partial'
779 bool testDiffItem(MathAtom const & at)
781 if (testString(at, "d") || testSymbol(at, "partial"))
784 // we may have d^n .../d and splitScripts() has not yet seen it
785 InsetMathScript const * sup = at->asScriptInset();
786 if (sup && !sup->hasDown() && sup->hasUp() && sup->nuc().size() == 1) {
787 MathAtom const & ma = sup->nuc().front();
788 return testString(ma, "d") || testSymbol(ma, "partial");
794 bool testDiffArray(MathData const & ar)
796 return ar.size() && testDiffItem(ar.front());
800 bool testDiffFrac(MathAtom const & at)
804 && testDiffArray(at->asFracInset()->cell(0))
805 && testDiffArray(at->asFracInset()->cell(1));
809 void extractDiff(MathData & ar)
811 //lyxerr << "\nDiffs from: " << ar << endl;
812 for (size_t i = 0; i < ar.size(); ++i) {
813 MathData::iterator it = ar.begin() + i;
815 // is this a "differential fraction"?
816 if (!testDiffFrac(*it))
819 InsetMathFrac const * f = (*it)->asFracInset();
821 lyxerr << "should not happen" << endl;
825 // create a proper diff inset
826 std::auto_ptr<InsetMathDiff> diff(new InsetMathDiff);
828 // collect function, let jt point behind last used item
829 MathData::iterator jt = it + 1;
831 MathData numer(f->cell(0));
833 if (numer.size() > 1 && numer[1]->asScriptInset()) {
834 // this is something like d^n f(x) / d... or d^n / d...
837 if (numer.size() > 2)
838 diff->cell(0) = MathData(numer.begin() + 2, numer.end());
840 jt = extractTerm(diff->cell(0), jt, ar.end());
842 // simply d f(x) / d... or d/d...
843 if (numer.size() > 1)
844 diff->cell(0) = MathData(numer.begin() + 1, numer.end());
846 jt = extractTerm(diff->cell(0), jt, ar.end());
849 // collect denominator parts
850 MathData denom(f->cell(1));
852 for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
854 MathData::iterator et
855 = find_if(dt + 1, denom.end(), &testDiffItem);
858 MathData::iterator st = et - 1;
859 InsetMathScript const * script = (*st)->asScriptInset();
860 if (script && script->hasUp()) {
861 // things like d.../dx^n
863 if (extractNumber(script->up(), mult)) {
864 //lyxerr << "mult: " << mult << endl;
865 for (int i = 0; i < mult; ++i)
866 diff->addDer(MathData(dt + 1, st));
870 diff->addDer(MathData(dt + 1, et));
876 ar.erase(it + 1, jt);
877 *it = MathAtom(diff.release());
879 //lyxerr << "\nDiffs to: " << ar << endl;
888 bool testRightArrow(MathAtom const & at)
890 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
895 // replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
896 // assume 'extractDelims' ran before
897 void extractLims(MathData & ar)
899 //lyxerr << "\nLimits from: " << ar << endl;
900 for (size_t i = 0; i < ar.size(); ++i) {
901 MathData::iterator it = ar.begin() + i;
903 // must be a script inset with a subscript (without superscript)
904 InsetMathScript const * sub = (*it)->asScriptInset();
905 if (!sub || !sub->hasDown() || sub->hasUp() || sub->nuc().size() != 1)
908 // is this a limit function?
909 if (!testSymbol(sub->nuc().front(), "lim"))
912 // subscript must contain a -> symbol
913 MathData const & s = sub->down();
914 MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
918 // the -> splits the subscript int x and x0
919 MathData x = MathData(s.begin(), st);
920 MathData x0 = MathData(st + 1, s.end());
922 // use something behind the script as core
924 MathData::iterator tt = extractTerm(f, it + 1, ar.end());
927 ar.erase(it + 1, tt);
929 // create a proper inset as replacement
930 *it = MathAtom(new InsetMathLim(f, x, x0));
932 //lyxerr << "\nLimits to: " << ar << endl;
940 void extractStructure(MathData & ar)
942 //lyxerr << "\nStructure from: " << ar << endl;
945 extractIntegrals(ar);
949 extractFunctions(ar);
955 //lyxerr << "\nStructure to: " << ar << endl;
959 void write(MathData const & dat, WriteStream & wi)
963 wi.firstitem() = true;
964 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it) {
966 wi.firstitem() = false;
971 void normalize(MathData const & ar, NormalStream & os)
973 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
974 (*it)->normalize(os);
978 void octave(MathData const & dat, OctaveStream & os)
981 extractStructure(ar);
982 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
987 void maple(MathData const & dat, MapleStream & os)
990 extractStructure(ar);
991 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
996 void maxima(MathData const & dat, MaximaStream & os)
999 extractStructure(ar);
1000 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1005 void mathematica(MathData const & dat, MathematicaStream & os)
1008 extractStructure(ar);
1009 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1010 (*it)->mathematica(os);
1014 void mathmlize(MathData const & dat, MathStream & os)
1017 extractStructure(ar);
1020 else if (ar.size() == 1)
1024 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1025 (*it)->mathmlize(os);
1035 std::string captureOutput(std::string const & cmd, std::string const & data)
1037 // In order to avoid parsing problems with command interpreters
1038 // we pass input data through a file
1039 FileName const cas_tmpfile(tempName(FileName(), "casinput"));
1040 if (cas_tmpfile.empty()) {
1041 lyxerr << "Warning: cannot create temporary file."
1043 return std::string();
1045 std::ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
1048 std::string command = cmd + " < "
1049 + quoteName(cas_tmpfile.toFilesystemEncoding());
1050 lyxerr << "calling: " << cmd
1051 << "\ninput: '" << data << "'" << endl;
1052 cmd_ret const ret = runCommand(command);
1053 cas_tmpfile.removeFile();
1057 size_t get_matching_brace(std::string const & str, size_t i)
1060 size_t n = str.size();
1062 i = str.find_first_of("{}", i+1);
1075 size_t get_matching_brace_back(std::string const & str, size_t i)
1079 i = str.find_last_of("{}", i-1);
1092 MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1094 odocstringstream os;
1095 MaximaStream ms(os);
1097 docstring expr = os.str();
1098 docstring const header = from_ascii("simpsum:true;");
1101 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1102 // try to fix missing '*' the hard way
1104 // > echo "2x;" | maxima
1106 // (C1) Incorrect syntax: x is not an infix operator
1110 lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1111 docstring full = header + "tex(" + expr + ");";
1112 out = captureOutput("maxima", to_utf8(full));
1114 // leave loop if expression syntax is probably ok
1115 if (out.find("Incorrect syntax") == npos)
1118 // search line with "Incorrect syntax"
1119 istringstream is(out);
1123 if (line.find("Incorrect syntax") != npos)
1127 // 2nd next line is the one with caret
1130 size_t pos = line.find('^');
1131 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1132 if (pos == npos || pos < 4)
1133 break; // caret position not found
1134 pos -= 4; // skip the "tex(" part
1135 if (expr[pos] == '*')
1136 break; // two '*' in a row are definitely bad
1137 expr.insert(pos, from_ascii("*"));
1140 vector<std::string> tmp = getVectorFromString(out, "$$");
1144 out = subst(tmp[1], "\\>", std::string());
1145 lyxerr << "output: '" << out << "'" << endl;
1147 // Ugly code that tries to make the result prettier
1148 size_t i = out.find("\\mathchoice");
1150 size_t j = get_matching_brace(out, i + 12);
1151 size_t k = get_matching_brace(out, j + 1);
1152 k = get_matching_brace(out, k + 1);
1153 k = get_matching_brace(out, k + 1);
1154 std::string mid = out.substr(i + 13, j - i - 13);
1155 if (mid.find("\\over") != npos)
1156 mid = '{' + mid + '}';
1157 out = out.substr(0,i)
1159 + out.substr(k + 1);
1160 //lyxerr << "output: " << out << endl;
1161 i = out.find("\\mathchoice", i);
1165 i = out.find("\\over");
1167 size_t j = get_matching_brace_back(out, i - 1);
1168 if (j == npos || j == 0)
1170 size_t k = get_matching_brace(out, i + 5);
1171 if (k == npos || k + 1 == out.size())
1173 out = out.substr(0,j - 1)
1175 + out.substr(j,i - j)
1176 + out.substr(i + 5,k - i - 4)
1177 + out.substr(k + 2);
1178 //lyxerr << "output: " << out << endl;
1179 i = out.find("\\over", i + 4);
1182 mathed_parse_cell(res, from_utf8(out));
1187 MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1189 std::string header = "readlib(latex):\n";
1191 // remove the \\it for variable names
1192 //"#`latex/csname_font` := `\\it `:"
1194 "`latex/csname_font` := ``:\n";
1196 // export matrices in (...) instead of [...]
1198 "`latex/latex/matrix` := "
1199 "subs(`[`=`(`, `]`=`)`,"
1200 "eval(`latex/latex/matrix`)):\n";
1202 // replace \\cdots with proper '*'
1204 "`latex/latex/*` := "
1205 "subs(`\\,`=`\\cdot `,"
1206 "eval(`latex/latex/*`)):\n";
1208 // remove spurious \\noalign{\\medskip} in matrix output
1210 "`latex/latex/matrix`:= "
1211 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1212 "eval(`latex/latex/matrix`)):\n";
1214 //"#`latex/latex/symbol` "
1215 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1217 std::string trailer = "quit;";
1218 odocstringstream os;
1221 std::string expr = to_utf8(os.str());
1222 lyxerr << "ar: '" << ar << "'\n"
1223 << "ms: '" << expr << "'" << endl;
1225 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1226 // try to fix missing '*' the hard way by using mint
1228 // ... > echo "1A;" | mint -i 1 -S -s -q
1231 // Probably missing an operator such as * p
1233 lyxerr << "checking expr: '" << expr << "'" << endl;
1234 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1236 break; // expression syntax is ok
1237 istringstream is(out);
1240 if (line.find("on line") != 0)
1241 break; // error message not identified
1243 size_t pos = line.find('^');
1244 if (pos == string::npos || pos < 15)
1245 break; // caret position not found
1246 pos -= 15; // skip the "on line ..." part
1247 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1248 break; // two '*' in a row are definitely bad
1249 expr.insert(pos, 1, '*');
1252 // FIXME UNICODE Is utf8 encoding correct?
1253 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1254 string out = captureOutput("maple -q", header + full + trailer);
1260 mathed_parse_cell(res, from_utf8(out));
1265 MathData pipeThroughOctave(docstring const &, MathData const & ar)
1267 odocstringstream os;
1268 OctaveStream vs(os);
1270 string expr = to_utf8(os.str());
1273 lyxerr << "pipe: ar: '" << ar << "'\n"
1274 << "pipe: expr: '" << expr << "'" << endl;
1276 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1278 // try to fix missing '*' the hard way
1280 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1283 lyxerr << "checking expr: '" << expr << "'" << endl;
1284 out = captureOutput("octave -q 2>&1", expr);
1285 lyxerr << "output: '" << out << "'" << endl;
1287 // leave loop if expression syntax is probably ok
1288 if (out.find("parse error:") == string::npos)
1291 // search line with single caret
1292 istringstream is(out);
1296 lyxerr << "skipping line: '" << line << "'" << endl;
1297 if (line.find(">>> ") != string::npos)
1301 // found line with error, next line is the one with caret
1303 size_t pos = line.find('^');
1304 lyxerr << "caret line: '" << line << "'" << endl;
1305 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1306 if (pos == string::npos || pos < 4)
1307 break; // caret position not found
1308 pos -= 4; // skip the ">>> " part
1309 if (expr[pos] == '*')
1310 break; // two '*' in a row are definitely bad
1311 expr.insert(pos, 1, '*');
1314 // remove 'ans = ' taking into account that there may be an
1315 // ansi control sequence before, such as '\033[?1034hans = '
1316 size_t i = out.find("ans = ");
1317 if (i == string::npos)
1319 out = out.substr(i + 6);
1321 // parse output as matrix or single number
1322 MathAtom at(new InsetMathArray(from_ascii("array"), from_utf8(out)));
1323 InsetMathArray const * mat = at->asArrayInset();
1325 if (mat->ncols() == 1 && mat->nrows() == 1)
1326 res.append(mat->cell(0));
1328 res.push_back(MathAtom(new InsetMathDelim(from_ascii("("), from_ascii(")"))));
1329 res.back().nucleus()->cell(0).push_back(at);
1335 string fromMathematicaName(string const & name)
1337 if (name == "Sin") return "sin";
1338 if (name == "Sinh") return "sinh";
1339 if (name == "ArcSin") return "arcsin";
1340 if (name == "Cos") return "cos";
1341 if (name == "Cosh") return "cosh";
1342 if (name == "ArcCos") return "arccos";
1343 if (name == "Tan") return "tan";
1344 if (name == "Tanh") return "tanh";
1345 if (name == "ArcTan") return "arctan";
1346 if (name == "Cot") return "cot";
1347 if (name == "Coth") return "coth";
1348 if (name == "Csc") return "csc";
1349 if (name == "Sec") return "sec";
1350 if (name == "Exp") return "exp";
1351 if (name == "Log") return "log";
1352 if (name == "Arg" ) return "arg";
1353 if (name == "Det" ) return "det";
1354 if (name == "GCD" ) return "gcd";
1355 if (name == "Max" ) return "max";
1356 if (name == "Min" ) return "min";
1357 if (name == "Erf" ) return "erf";
1358 if (name == "Erfc" ) return "erfc";
1363 void prettifyMathematicaOutput(string & out, string const & macroName,
1364 bool roman, bool translate)
1366 string const macro = "\\" + macroName + "{";
1367 size_t const len = macro.length();
1368 size_t i = out.find(macro);
1371 size_t const j = get_matching_brace(out, i + len);
1372 string const name = out.substr(i + len, j - i - len);
1373 out = out.substr(0, i)
1374 + (roman ? "\\mathrm{" : "")
1375 + (translate ? fromMathematicaName(name) : name)
1376 + out.substr(roman ? j : j + 1);
1377 //lyxerr << "output: " << out << endl;
1378 i = out.find(macro, i);
1383 MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1385 odocstringstream os;
1386 MathematicaStream ms(os);
1388 // FIXME UNICODE Is utf8 encoding correct?
1389 string const expr = to_utf8(os.str());
1392 lyxerr << "expr: '" << expr << "'" << endl;
1394 string const full = "TeXForm[" + expr + "]";
1395 out = captureOutput("math", full);
1396 lyxerr << "output: '" << out << "'" << endl;
1398 size_t pos1 = out.find("Out[1]//TeXForm= ");
1399 size_t pos2 = out.find("In[2]:=");
1401 if (pos1 == string::npos || pos2 == string::npos)
1404 // get everything from pos1+17 to pos2
1405 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1406 out = subst(subst(out, '\r', ' '), '\n', ' ');
1408 // tries to make the result prettier
1409 prettifyMathematicaOutput(out, "Mfunction", true, true);
1410 prettifyMathematicaOutput(out, "Muserfunction", true, false);
1411 prettifyMathematicaOutput(out, "Mvariable", false, false);
1414 mathed_parse_cell(res, from_utf8(out));
1421 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1422 MathData const & ar)
1424 if (lang == "octave")
1425 return pipeThroughOctave(extra, ar);
1427 if (lang == "maxima")
1428 return pipeThroughMaxima(extra, ar);
1430 if (lang == "maple")
1431 return pipeThroughMaple(extra, ar);
1433 if (lang == "mathematica")
1434 return pipeThroughMathematica(extra, ar);
1436 // create normalized expression
1437 odocstringstream os;
1438 NormalStream ns(os);
1439 os << '[' << extra << ' ';
1442 // FIXME UNICODE Is utf8 encoding correct?
1443 string data = to_utf8(os.str());
1445 // search external script
1446 support::FileName const file = libFileSearch("mathed", "extern_" + lang);
1448 lyxerr << "converter to '" << lang << "' not found" << endl;
1452 // run external sript
1453 string out = captureOutput(file.absFilename(), data);
1455 mathed_parse_cell(res, from_utf8(out));