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"
19 #include "InsetMathArray.h"
20 #include "InsetMathChar.h"
21 #include "InsetMathDelim.h"
22 #include "InsetMathDiff.h"
23 #include "InsetMathExFunc.h"
24 #include "InsetMathExInt.h"
25 #include "InsetMathFont.h"
26 #include "InsetMathFrac.h"
27 #include "InsetMathLim.h"
28 #include "InsetMathMatrix.h"
29 #include "InsetMathNumber.h"
30 #include "InsetMathScript.h"
31 #include "InsetMathString.h"
32 #include "InsetMathSymbol.h"
34 #include "MathParser.h"
35 #include "MathStream.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/FileName.h"
40 #include "support/filetools.h"
41 #include "support/lstrings.h"
49 using namespace lyx::support;
64 static char const * function_names[] = {
65 "arccos", "arcsin", "arctan", "arg", "bmod",
66 "cos", "cosh", "cot", "coth", "csc", "deg",
67 "det", "dim", "exp", "gcd", "hom", "inf", "ker",
68 "lg", "lim", "liminf", "limsup", "ln", "log",
69 "max", "min", "sec", "sin", "sinh", "sup",
70 "tan", "tanh", "Pr", 0
73 static size_t const npos = lyx::docstring::npos;
75 // define a function for tests
76 typedef bool TestItemFunc(MathAtom const &);
78 // define a function for replacing subexpressions
79 typedef MathAtom ReplaceArgumentFunc(const MathData & ar);
83 // try to extract a super/subscript
84 // modify iterator position to point behind the thing
85 bool extractScript(MathData & ar,
86 MathData::iterator & pos, MathData::iterator last, bool superscript)
88 // nothing to get here
92 // is this a scriptinset?
93 if (!(*pos)->asScriptInset())
96 // do we want superscripts only?
97 if (superscript && !(*pos)->asScriptInset()->hasUp())
100 // it is a scriptinset, use it.
107 // try to extract an "argument" to some function.
108 // returns position behind the argument
109 MathData::iterator extractArgument(MathData & ar,
110 MathData::iterator pos, MathData::iterator last,
111 ExternalMath kind, bool function = false)
113 // nothing to get here
117 // something delimited _is_ an argument
118 if ((*pos)->asDelimInset()) {
119 // leave out delimiters if this is a function argument
120 if (function && kind != MATHML) {
121 MathData const & arg = (*pos)->asDelimInset()->cell(0);
122 MathData::const_iterator cur = arg.begin();
123 MathData::const_iterator end = arg.end();
125 ar.push_back(*cur++);
131 // if there's one, get following superscript only if this
132 // isn't a function argument
134 extractScript(ar, pos, last, true);
138 // always take the first thing, no matter what it is
141 // go ahead if possible
146 // if the next item is a super/subscript, it most certainly belongs
147 // to the thing we have
148 extractScript(ar, pos, last, false);
152 // but it might be more than that.
153 // FIXME: not implemented
154 //for (MathData::iterator it = pos + 1; it != last; ++it) {
155 // // always take the first thing, no matter
157 // ar.push_back(*it);
165 // returns sequence of char with same code starting at it up to end
166 // it might be less, though...
167 docstring charSequence
168 (MathData::const_iterator it, MathData::const_iterator end)
171 for (; it != end && (*it)->asCharInset(); ++it)
172 s += (*it)->getChar();
177 void extractStrings(MathData & ar)
179 //lyxerr << "\nStrings from: " << ar << endl;
180 for (size_t i = 0; i < ar.size(); ++i) {
181 if (!ar[i]->asCharInset())
183 docstring s = charSequence(ar.begin() + i, ar.end());
184 ar[i] = MathAtom(new InsetMathString(s));
185 ar.erase(i + 1, i + s.size());
187 //lyxerr << "\nStrings to: " << ar << endl;
191 void extractMatrices(MathData & ar)
193 //lyxerr << "\nMatrices from: " << ar << endl;
194 // first pass for explicitly delimited stuff
195 for (size_t i = 0; i < ar.size(); ++i) {
196 if (!ar[i]->asDelimInset())
198 MathData const & arr = ar[i]->asDelimInset()->cell(0);
201 if (!arr.front()->asGridInset())
203 ar[i] = MathAtom(new InsetMathMatrix(*(arr.front()->asGridInset())));
206 // second pass for AMS "pmatrix" etc
207 for (size_t i = 0; i < ar.size(); ++i)
208 if (ar[i]->asAMSArrayInset())
209 ar[i] = MathAtom(new InsetMathMatrix(*(ar[i]->asGridInset())));
210 //lyxerr << "\nMatrices to: " << ar << endl;
214 // convert this inset somehow to a string
215 bool extractString(MathAtom const & at, docstring & str)
218 str = docstring(1, at->getChar());
221 if (at->asStringInset()) {
222 str = at->asStringInset()->str();
229 // is this a known function?
230 bool isKnownFunction(docstring const & str)
232 for (int i = 0; function_names[i]; ++i) {
233 if (str == function_names[i])
240 // extract a function name from this inset
241 bool extractFunctionName(MathAtom const & at, docstring & str)
243 if (at->asSymbolInset()) {
244 str = at->asSymbolInset()->name();
245 return isKnownFunction(str);
247 if (at->asUnknownInset()) {
248 // assume it is well known...
252 if (at->asFontInset() && at->name() == "mathrm") {
253 // assume it is well known...
254 MathData const & ar = at->asFontInset()->cell(0);
255 str = charSequence(ar.begin(), ar.end());
256 return ar.size() == str.size();
262 bool testString(MathAtom const & at, docstring const & str)
265 return extractString(at, s) && str == s;
269 bool testString(MathAtom const & at, char const * const str)
271 return testString(at, from_ascii(str));
274 // search end of nested sequence
275 MathData::iterator endNestSearch(
276 MathData::iterator it,
277 MathData::iterator last,
278 TestItemFunc testOpen,
279 TestItemFunc testClose
282 for (int level = 0; it != last; ++it) {
294 // replace nested sequences by a real Insets
297 TestItemFunc testOpen,
298 TestItemFunc testClose,
299 ReplaceArgumentFunc replaceArg)
301 Buffer * buf = ar.buffer();
302 // use indices rather than iterators for the loop because we are going
303 // to modify the array.
304 for (size_t i = 0; i < ar.size(); ++i) {
305 // check whether this is the begin of the sequence
306 if (!testOpen(ar[i]))
309 // search end of sequence
310 MathData::iterator it = ar.begin() + i;
311 MathData::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
315 // replace the original stuff by the new inset
316 ar[i] = replaceArg(MathData(buf, it + 1, jt));
317 ar.erase(it + 1, jt + 1);
324 // split scripts into seperate super- and subscript insets. sub goes in
328 void splitScripts(MathData & ar)
330 Buffer * buf = ar.buffer();
331 //lyxerr << "\nScripts from: " << ar << endl;
332 for (size_t i = 0; i < ar.size(); ++i) {
333 InsetMathScript const * script = ar[i]->asScriptInset();
335 // is this a script inset and do we also have a superscript?
336 if (!script || !script->hasUp())
339 // we must have a nucleus if we only have a superscript
340 if (!script->hasDown() && script->nuc().size() == 0)
343 if (script->nuc().size() == 1) {
344 // leave alone sums and integrals
345 InsetMathSymbol const * sym =
346 script->nuc().front()->asSymbolInset();
347 if (sym && (sym->name() == "sum" || sym->name() == "int"))
351 // create extra script inset and move superscript over
352 InsetMathScript * p = ar[i].nucleus()->asScriptInset();
353 auto_ptr<InsetMathScript> q(new InsetMathScript(buf, true));
354 swap(q->up(), p->up());
355 p->removeScript(true);
357 // if we don't have a subscript, get rid of the ScriptInset
358 if (!script->hasDown()) {
359 MathData arg(p->nuc());
360 MathData::const_iterator it = arg.begin();
361 MathData::const_iterator et = arg.end();
364 ar.insert(i++, *it++);
368 // insert new inset behind
369 ar.insert(i, MathAtom(q.release()));
371 //lyxerr << "\nScripts to: " << ar << endl;
379 void extractExps(MathData & ar)
381 Buffer * buf = ar.buffer();
382 //lyxerr << "\nExps from: " << ar << endl;
383 for (size_t i = 0; i + 1 < ar.size(); ++i) {
385 if (ar[i]->getChar() != 'e')
388 // we need an exponent but no subscript
389 InsetMathScript const * sup = ar[i + 1]->asScriptInset();
390 if (!sup || sup->hasDown())
393 // create a proper exp-inset as replacement
394 ar[i] = MathAtom(new InsetMathExFunc(buf, from_ascii("exp"), sup->cell(1)));
397 //lyxerr << "\nExps to: " << ar << endl;
402 // extract det(...) from |matrix|
404 void extractDets(MathData & ar)
406 Buffer * buf = ar.buffer();
407 //lyxerr << "\ndet from: " << ar << endl;
408 for (MathData::iterator it = ar.begin(); it != ar.end(); ++it) {
409 InsetMathDelim const * del = (*it)->asDelimInset();
414 *it = MathAtom(new InsetMathExFunc(buf, from_ascii("det"), del->cell(0)));
416 //lyxerr << "\ndet to: " << ar << endl;
424 bool isDigitOrSimilar(char_type c)
426 return ('0' <= c && c <= '9') || c == '.';
430 // returns sequence of digits
431 docstring digitSequence
432 (MathData::const_iterator it, MathData::const_iterator end)
435 for (; it != end && (*it)->asCharInset(); ++it) {
436 if (!isDigitOrSimilar((*it)->getChar()))
438 s += (*it)->getChar();
444 void extractNumbers(MathData & ar)
446 //lyxerr << "\nNumbers from: " << ar << endl;
447 for (size_t i = 0; i < ar.size(); ++i) {
448 if (!ar[i]->asCharInset())
450 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
453 docstring s = digitSequence(ar.begin() + i, ar.end());
455 ar[i] = MathAtom(new InsetMathNumber(s));
456 ar.erase(i + 1, i + s.size());
458 //lyxerr << "\nNumbers to: " << ar << endl;
467 bool testOpenParen(MathAtom const & at)
469 return testString(at, "(");
473 bool testCloseParen(MathAtom const & at)
475 return testString(at, ")");
479 MathAtom replaceParenDelims(const MathData & ar)
481 return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
482 from_ascii("("), from_ascii(")"), ar));
486 bool testOpenBracket(MathAtom const & at)
488 return testString(at, "[");
492 bool testCloseBracket(MathAtom const & at)
494 return testString(at, "]");
498 MathAtom replaceBracketDelims(const MathData & ar)
500 return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
501 from_ascii("["), from_ascii("]"), ar));
505 // replace '('...')' and '['...']' sequences by a real InsetMathDelim
506 void extractDelims(MathData & ar)
508 //lyxerr << "\nDelims from: " << ar << endl;
509 replaceNested(ar, testOpenParen, testCloseParen, replaceParenDelims);
510 replaceNested(ar, testOpenBracket, testCloseBracket, replaceBracketDelims);
511 //lyxerr << "\nDelims to: " << ar << endl;
517 // search well-known functions
521 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real InsetMathExFunc
522 // assume 'extractDelims' ran before
523 void extractFunctions(MathData & ar, ExternalMath kind)
525 // we need at least two items...
529 Buffer * buf = ar.buffer();
531 //lyxerr << "\nFunctions from: " << ar << endl;
532 for (size_t i = 0; i + 1 < ar.size(); ++i) {
533 MathData::iterator it = ar.begin() + i;
534 MathData::iterator jt = it + 1;
538 // it certainly is if it is well known...
539 if (!extractFunctionName(*it, name)) {
540 // is this a user defined function?
541 // it it probably not, if it doesn't have a name.
542 if (!extractString(*it, name))
544 // it is not if it has no argument
547 // guess so, if this is followed by
548 // a DelimInset with a single item in the cell
549 InsetMathDelim const * del = (*jt)->asDelimInset();
550 if (!del || del->cell(0).size() != 1)
552 // fall trough into main branch
555 // do we have an exponent like in
556 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
558 extractScript(exp, jt, ar.end(), true);
560 // create a proper inset as replacement
561 auto_ptr<InsetMathExFunc> p(new InsetMathExFunc(buf, name));
563 // jt points to the "argument". Get hold of this.
564 MathData::iterator st =
565 extractArgument(p->cell(0), jt, ar.end(), kind, true);
567 // replace the function name by a real function inset
568 *it = MathAtom(p.release());
570 // remove the source of the argument from the array
571 ar.erase(it + 1, st);
573 // re-insert exponent
574 ar.insert(i + 1, exp);
575 //lyxerr << "\nFunctions to: " << ar << endl;
584 bool testSymbol(MathAtom const & at, docstring const & name)
586 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
590 bool testSymbol(MathAtom const & at, char const * const name)
592 return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
596 bool testIntSymbol(MathAtom const & at)
598 return testSymbol(at, from_ascii("int"));
602 bool testIntegral(MathAtom const & at)
606 ( at->asScriptInset()
607 && at->asScriptInset()->nuc().size()
608 && testIntSymbol(at->asScriptInset()->nuc().back()) );
613 bool testIntDiff(MathAtom const & at)
615 return testString(at, "d");
619 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
620 // assume 'extractDelims' ran before
621 void extractIntegrals(MathData & ar, ExternalMath kind)
623 // we need at least three items...
627 Buffer * buf = ar.buffer();
629 //lyxerr << "\nIntegrals from: " << ar << endl;
630 for (size_t i = 0; i + 1 < ar.size(); ++i) {
631 MathData::iterator it = ar.begin() + i;
634 MathData::iterator jt =
635 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
637 // something sensible found?
641 // is this a integral name?
642 if (!testIntegral(*it))
645 // core ist part from behind the scripts to the 'd'
646 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("int")));
648 // handle scripts if available
649 if (!testIntSymbol(*it)) {
650 p->cell(2) = (*it)->asScriptInset()->down();
651 p->cell(3) = (*it)->asScriptInset()->up();
653 p->cell(0) = MathData(buf, it + 1, jt);
655 // use the "thing" behind the 'd' as differential
656 MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end(), kind);
659 ar.erase(it + 1, tt);
660 *it = MathAtom(p.release());
662 //lyxerr << "\nIntegrals to: " << ar << endl;
666 bool testTermDelimiter(MathAtom const & at)
668 return testString(at, "+") || testString(at, "-");
672 // try to extract a "term", i.e., something delimited by '+' or '-'.
673 // returns position behind the term
674 MathData::iterator extractTerm(MathData & ar,
675 MathData::iterator pos, MathData::iterator last)
677 while (pos != last && !testTermDelimiter(*pos)) {
690 bool testEqualSign(MathAtom const & at)
692 return testString(at, "=");
696 bool testSumSymbol(MathAtom const & p)
698 return testSymbol(p, from_ascii("sum"));
702 bool testSum(MathAtom const & at)
706 ( at->asScriptInset()
707 && at->asScriptInset()->nuc().size()
708 && testSumSymbol(at->asScriptInset()->nuc().back()) );
712 // replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
713 // assume 'extractDelims' ran before
714 void extractSums(MathData & ar)
716 // we need at least two items...
720 Buffer * buf = ar.buffer();
722 //lyxerr << "\nSums from: " << ar << endl;
723 for (size_t i = 0; i + 1 < ar.size(); ++i) {
724 MathData::iterator it = ar.begin() + i;
726 // is this a sum name?
730 // create a proper inset as replacement
731 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("sum")));
733 // collect lower bound and summation index
734 InsetMathScript const * sub = ar[i]->asScriptInset();
735 if (sub && sub->hasDown()) {
736 // try to figure out the summation index from the subscript
737 MathData const & ar = sub->down();
738 MathData::const_iterator xt =
739 find_if(ar.begin(), ar.end(), &testEqualSign);
740 if (xt != ar.end()) {
741 // we found a '=', use everything in front of that as index,
742 // and everything behind as lower index
743 p->cell(1) = MathData(buf, ar.begin(), xt);
744 p->cell(2) = MathData(buf, xt + 1, ar.end());
746 // use everything as summation index, don't use scripts.
751 // collect upper bound
752 if (sub && sub->hasUp())
753 p->cell(3) = sub->up();
755 // use something behind the script as core
756 MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
759 ar.erase(it + 1, tt);
760 *it = MathAtom(p.release());
762 //lyxerr << "\nSums to: " << ar << endl;
767 // search differential stuff
770 // tests for 'd' or '\partial'
771 bool testDiffItem(MathAtom const & at)
773 if (testString(at, "d") || testSymbol(at, "partial"))
776 // we may have d^n .../d and splitScripts() has not yet seen it
777 InsetMathScript const * sup = at->asScriptInset();
778 if (sup && !sup->hasDown() && sup->hasUp() && sup->nuc().size() == 1) {
779 MathAtom const & ma = sup->nuc().front();
780 return testString(ma, "d") || testSymbol(ma, "partial");
786 bool testDiffArray(MathData const & ar)
788 return ar.size() && testDiffItem(ar.front());
792 bool testDiffFrac(MathAtom const & at)
796 && testDiffArray(at->asFracInset()->cell(0))
797 && testDiffArray(at->asFracInset()->cell(1));
801 void extractDiff(MathData & ar)
803 Buffer * buf = ar.buffer();
804 //lyxerr << "\nDiffs from: " << ar << endl;
805 for (size_t i = 0; i < ar.size(); ++i) {
806 MathData::iterator it = ar.begin() + i;
808 // is this a "differential fraction"?
809 if (!testDiffFrac(*it))
812 InsetMathFrac const * f = (*it)->asFracInset();
814 lyxerr << "should not happen" << endl;
818 // create a proper diff inset
819 auto_ptr<InsetMathDiff> diff(new InsetMathDiff(buf));
821 // collect function, let jt point behind last used item
822 MathData::iterator jt = it + 1;
824 MathData numer(f->cell(0));
826 if (numer.size() > 1 && numer[1]->asScriptInset()) {
827 // this is something like d^n f(x) / d... or d^n / d...
830 if (numer.size() > 2)
831 diff->cell(0) = MathData(buf, numer.begin() + 2, numer.end());
833 jt = extractTerm(diff->cell(0), jt, ar.end());
835 // simply d f(x) / d... or d/d...
836 if (numer.size() > 1)
837 diff->cell(0) = MathData(buf, numer.begin() + 1, numer.end());
839 jt = extractTerm(diff->cell(0), jt, ar.end());
842 // collect denominator parts
843 MathData denom(f->cell(1));
845 for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
847 MathData::iterator et
848 = find_if(dt + 1, denom.end(), &testDiffItem);
851 MathData::iterator st = et - 1;
852 InsetMathScript const * script = (*st)->asScriptInset();
853 if (script && script->hasUp()) {
854 // things like d.../dx^n
856 if (extractNumber(script->up(), mult)) {
857 //lyxerr << "mult: " << mult << endl;
858 for (int i = 0; i < mult; ++i)
859 diff->addDer(MathData(buf, dt + 1, st));
863 diff->addDer(MathData(buf, dt + 1, et));
869 ar.erase(it + 1, jt);
870 *it = MathAtom(diff.release());
872 //lyxerr << "\nDiffs to: " << ar << endl;
881 bool testRightArrow(MathAtom const & at)
883 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
888 // replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
889 // assume 'extractDelims' ran before
890 void extractLims(MathData & ar)
892 Buffer * buf = ar.buffer();
893 //lyxerr << "\nLimits from: " << ar << endl;
894 for (size_t i = 0; i < ar.size(); ++i) {
895 MathData::iterator it = ar.begin() + i;
897 // must be a script inset with a subscript (without superscript)
898 InsetMathScript const * sub = (*it)->asScriptInset();
899 if (!sub || !sub->hasDown() || sub->hasUp() || sub->nuc().size() != 1)
902 // is this a limit function?
903 if (!testSymbol(sub->nuc().front(), "lim"))
906 // subscript must contain a -> symbol
907 MathData const & s = sub->down();
908 MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
912 // the -> splits the subscript int x and x0
913 MathData x = MathData(buf, s.begin(), st);
914 MathData x0 = MathData(buf, st + 1, s.end());
916 // use something behind the script as core
918 MathData::iterator tt = extractTerm(f, it + 1, ar.end());
921 ar.erase(it + 1, tt);
923 // create a proper inset as replacement
924 *it = MathAtom(new InsetMathLim(buf, f, x, x0));
926 //lyxerr << "\nLimits to: " << ar << endl;
934 void extractStructure(MathData & ar, ExternalMath kind)
936 //lyxerr << "\nStructure from: " << ar << endl;
939 extractIntegrals(ar, kind);
943 extractFunctions(ar, kind);
950 //lyxerr << "\nStructure to: " << ar << endl;
956 string captureOutput(string const & cmd, string const & data)
958 // In order to avoid parsing problems with command interpreters
959 // we pass input data through a file
960 FileName const cas_tmpfile = FileName::tempName("casinput");
961 if (cas_tmpfile.empty()) {
962 lyxerr << "Warning: cannot create temporary file."
966 ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
969 string command = cmd + " < "
970 + quoteName(cas_tmpfile.toFilesystemEncoding());
971 lyxerr << "calling: " << cmd
972 << "\ninput: '" << data << "'" << endl;
973 cmd_ret const ret = runCommand(command);
974 cas_tmpfile.removeFile();
978 size_t get_matching_brace(string const & str, size_t i)
981 size_t n = str.size();
983 i = str.find_first_of("{}", i+1);
996 size_t get_matching_brace_back(string const & str, size_t i)
1000 i = str.find_last_of("{}", i-1);
1013 MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1015 odocstringstream os;
1016 MaximaStream ms(os);
1018 docstring expr = os.str();
1019 docstring const header = from_ascii("simpsum:true;");
1022 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1023 // try to fix missing '*' the hard way
1025 // > echo "2x;" | maxima
1027 // (C1) Incorrect syntax: x is not an infix operator
1031 lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1032 docstring full = header + "tex(" + expr + ");";
1033 out = captureOutput("maxima", to_utf8(full));
1035 // leave loop if expression syntax is probably ok
1036 if (out.find("Incorrect syntax") == npos)
1039 // search line with "Incorrect syntax"
1040 istringstream is(out);
1044 if (line.find("Incorrect syntax") != npos)
1048 // 2nd next line is the one with caret
1051 size_t pos = line.find('^');
1052 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1053 if (pos == npos || pos < 4)
1054 break; // caret position not found
1055 pos -= 4; // skip the "tex(" part
1056 if (expr[pos] == '*')
1057 break; // two '*' in a row are definitely bad
1058 expr.insert(pos, from_ascii("*"));
1061 vector<string> tmp = getVectorFromString(out, "$$");
1065 out = subst(tmp[1], "\\>", string());
1066 lyxerr << "output: '" << out << "'" << endl;
1068 // Ugly code that tries to make the result prettier
1069 size_t i = out.find("\\mathchoice");
1071 size_t j = get_matching_brace(out, i + 12);
1072 size_t k = get_matching_brace(out, j + 1);
1073 k = get_matching_brace(out, k + 1);
1074 k = get_matching_brace(out, k + 1);
1075 string mid = out.substr(i + 13, j - i - 13);
1076 if (mid.find("\\over") != npos)
1077 mid = '{' + mid + '}';
1078 out = out.substr(0, i)
1080 + out.substr(k + 1);
1081 //lyxerr << "output: " << out << endl;
1082 i = out.find("\\mathchoice", i);
1086 i = out.find("\\over");
1088 size_t j = get_matching_brace_back(out, i - 1);
1089 if (j == npos || j == 0)
1091 size_t k = get_matching_brace(out, i + 5);
1092 if (k == npos || k + 1 == out.size())
1094 out = out.substr(0, j - 1)
1096 + out.substr(j, i - j)
1097 + out.substr(i + 5, k - i - 4)
1098 + out.substr(k + 2);
1099 //lyxerr << "output: " << out << endl;
1100 i = out.find("\\over", i + 4);
1103 mathed_parse_cell(res, from_utf8(out));
1108 MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1110 string header = "readlib(latex):\n";
1112 // remove the \\it for variable names
1113 //"#`latex/csname_font` := `\\it `:"
1115 "`latex/csname_font` := ``:\n";
1117 // export matrices in (...) instead of [...]
1119 "`latex/latex/matrix` := "
1120 "subs(`[`=`(`, `]`=`)`,"
1121 "eval(`latex/latex/matrix`)):\n";
1123 // replace \\cdots with proper '*'
1125 "`latex/latex/*` := "
1126 "subs(`\\,`=`\\cdot `,"
1127 "eval(`latex/latex/*`)):\n";
1129 // remove spurious \\noalign{\\medskip} in matrix output
1131 "`latex/latex/matrix`:= "
1132 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1133 "eval(`latex/latex/matrix`)):\n";
1135 //"#`latex/latex/symbol` "
1136 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1138 string trailer = "quit;";
1139 odocstringstream os;
1142 string expr = to_utf8(os.str());
1143 lyxerr << "ar: '" << ar << "'\n"
1144 << "ms: '" << expr << "'" << endl;
1146 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1147 // try to fix missing '*' the hard way by using mint
1149 // ... > echo "1A;" | mint -i 1 -S -s -q
1152 // Probably missing an operator such as * p
1154 lyxerr << "checking expr: '" << expr << "'" << endl;
1155 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1157 break; // expression syntax is ok
1158 istringstream is(out);
1161 if (line.find("on line") != 0)
1162 break; // error message not identified
1164 size_t pos = line.find('^');
1165 if (pos == string::npos || pos < 15)
1166 break; // caret position not found
1167 pos -= 15; // skip the "on line ..." part
1168 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1169 break; // two '*' in a row are definitely bad
1170 expr.insert(pos, 1, '*');
1173 // FIXME UNICODE Is utf8 encoding correct?
1174 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1175 string out = captureOutput("maple -q", header + full + trailer);
1181 mathed_parse_cell(res, from_utf8(out));
1186 MathData pipeThroughOctave(docstring const &, MathData const & ar)
1188 odocstringstream os;
1189 OctaveStream vs(os);
1191 string expr = to_utf8(os.str());
1194 Buffer * buf = const_cast<Buffer *>(ar.buffer());
1195 lyxerr << "pipe: ar: '" << ar << "'\n"
1196 << "pipe: expr: '" << expr << "'" << endl;
1198 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1200 // try to fix missing '*' the hard way
1202 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1205 lyxerr << "checking expr: '" << expr << "'" << endl;
1206 out = captureOutput("octave -q 2>&1", expr);
1207 lyxerr << "output: '" << out << "'" << endl;
1209 // leave loop if expression syntax is probably ok
1210 if (out.find("parse error:") == string::npos)
1213 // search line with single caret
1214 istringstream is(out);
1218 lyxerr << "skipping line: '" << line << "'" << endl;
1219 if (line.find(">>> ") != string::npos)
1223 // found line with error, next line is the one with caret
1225 size_t pos = line.find('^');
1226 lyxerr << "caret line: '" << line << "'" << endl;
1227 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1228 if (pos == string::npos || pos < 4)
1229 break; // caret position not found
1230 pos -= 4; // skip the ">>> " part
1231 if (expr[pos] == '*')
1232 break; // two '*' in a row are definitely bad
1233 expr.insert(pos, 1, '*');
1236 // remove 'ans = ' taking into account that there may be an
1237 // ansi control sequence before, such as '\033[?1034hans = '
1238 size_t i = out.find("ans = ");
1239 if (i == string::npos)
1241 out = out.substr(i + 6);
1243 // parse output as matrix or single number
1244 MathAtom at(new InsetMathArray(buf, from_ascii("array"), from_utf8(out)));
1245 InsetMathArray const * mat = at->asArrayInset();
1247 if (mat->ncols() == 1 && mat->nrows() == 1)
1248 res.append(mat->cell(0));
1250 res.push_back(MathAtom(
1251 new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
1252 res.back().nucleus()->cell(0).push_back(at);
1258 string fromMathematicaName(string const & name)
1260 if (name == "Sin") return "sin";
1261 if (name == "Sinh") return "sinh";
1262 if (name == "ArcSin") return "arcsin";
1263 if (name == "Cos") return "cos";
1264 if (name == "Cosh") return "cosh";
1265 if (name == "ArcCos") return "arccos";
1266 if (name == "Tan") return "tan";
1267 if (name == "Tanh") return "tanh";
1268 if (name == "ArcTan") return "arctan";
1269 if (name == "Cot") return "cot";
1270 if (name == "Coth") return "coth";
1271 if (name == "Csc") return "csc";
1272 if (name == "Sec") return "sec";
1273 if (name == "Exp") return "exp";
1274 if (name == "Log") return "log";
1275 if (name == "Arg" ) return "arg";
1276 if (name == "Det" ) return "det";
1277 if (name == "GCD" ) return "gcd";
1278 if (name == "Max" ) return "max";
1279 if (name == "Min" ) return "min";
1280 if (name == "Erf" ) return "erf";
1281 if (name == "Erfc" ) return "erfc";
1286 void prettifyMathematicaOutput(string & out, string const & macroName,
1287 bool roman, bool translate)
1289 string const macro = "\\" + macroName + "{";
1290 size_t const len = macro.length();
1291 size_t i = out.find(macro);
1294 size_t const j = get_matching_brace(out, i + len);
1295 string const name = out.substr(i + len, j - i - len);
1296 out = out.substr(0, i)
1297 + (roman ? "\\mathrm{" : "")
1298 + (translate ? fromMathematicaName(name) : name)
1299 + out.substr(roman ? j : j + 1);
1300 //lyxerr << "output: " << out << endl;
1301 i = out.find(macro, i);
1306 MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1308 odocstringstream os;
1309 MathematicaStream ms(os);
1311 // FIXME UNICODE Is utf8 encoding correct?
1312 string const expr = to_utf8(os.str());
1315 lyxerr << "expr: '" << expr << "'" << endl;
1317 string const full = "TeXForm[" + expr + "]";
1318 out = captureOutput("math", full);
1319 lyxerr << "output: '" << out << "'" << endl;
1321 size_t pos1 = out.find("Out[1]//TeXForm= ");
1322 size_t pos2 = out.find("In[2]:=");
1324 if (pos1 == string::npos || pos2 == string::npos)
1327 // get everything from pos1+17 to pos2
1328 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1329 out = subst(subst(out, '\r', ' '), '\n', ' ');
1331 // tries to make the result prettier
1332 prettifyMathematicaOutput(out, "Mfunction", true, true);
1333 prettifyMathematicaOutput(out, "Muserfunction", true, false);
1334 prettifyMathematicaOutput(out, "Mvariable", false, false);
1337 mathed_parse_cell(res, from_utf8(out));
1345 void write(MathData const & dat, WriteStream & wi)
1349 wi.firstitem() = true;
1350 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it) {
1352 wi.firstitem() = false;
1357 void normalize(MathData const & ar, NormalStream & os)
1359 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1360 (*it)->normalize(os);
1364 void octave(MathData const & dat, OctaveStream & os)
1367 extractStructure(ar, OCTAVE);
1368 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1373 void maple(MathData const & dat, MapleStream & os)
1376 extractStructure(ar, MAPLE);
1377 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1382 void maxima(MathData const & dat, MaximaStream & os)
1385 extractStructure(ar, MAXIMA);
1386 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1391 void mathematica(MathData const & dat, MathematicaStream & os)
1394 extractStructure(ar, MATHEMATICA);
1395 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1396 (*it)->mathematica(os);
1400 void mathmlize(MathData const & dat, MathStream & os)
1403 extractStructure(ar, MATHML);
1406 else if (ar.size() == 1)
1410 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1411 (*it)->mathmlize(os);
1416 // convert this inset somehow to a number
1417 bool extractNumber(MathData const & ar, int & i)
1419 idocstringstream is(charSequence(ar.begin(), ar.end()));
1425 bool extractNumber(MathData const & ar, double & d)
1427 idocstringstream is(charSequence(ar.begin(), ar.end()));
1433 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1434 MathData const & ar)
1436 if (lang == "octave")
1437 return pipeThroughOctave(extra, ar);
1439 if (lang == "maxima")
1440 return pipeThroughMaxima(extra, ar);
1442 if (lang == "maple")
1443 return pipeThroughMaple(extra, ar);
1445 if (lang == "mathematica")
1446 return pipeThroughMathematica(extra, ar);
1448 // create normalized expression
1449 odocstringstream os;
1450 NormalStream ns(os);
1451 os << '[' << extra << ' ';
1454 // FIXME UNICODE Is utf8 encoding correct?
1455 string data = to_utf8(os.str());
1457 // search external script
1458 FileName const file = libFileSearch("mathed", "extern_" + lang);
1460 lyxerr << "converter to '" << lang << "' not found" << endl;
1464 // run external sript
1465 string out = captureOutput(file.absFilename(), data);
1467 mathed_parse_cell(res, from_utf8(out));