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 "InsetMathAMSArray.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/debug.h"
39 #include "support/docstream.h"
40 #include "support/FileName.h"
41 #include "support/filetools.h"
42 #include "support/lstrings.h"
50 using namespace lyx::support;
66 static char const * function_names[] = {
67 "arccos", "arcsin", "arctan", "arg", "bmod",
68 "cos", "cosh", "cot", "coth", "csc", "deg",
69 "det", "dim", "exp", "gcd", "hom", "inf", "ker",
70 "lg", "lim", "liminf", "limsup", "ln", "log",
71 "max", "min", "sec", "sin", "sinh", "sup",
72 "tan", "tanh", "Pr", 0
75 static size_t const npos = lyx::docstring::npos;
77 // define a function for tests
78 typedef bool TestItemFunc(MathAtom const &);
80 // define a function for replacing subexpressions
81 typedef MathAtom ReplaceArgumentFunc(const MathData & ar);
85 // try to extract a super/subscript
86 // modify iterator position to point behind the thing
87 bool extractScript(MathData & ar,
88 MathData::iterator & pos, MathData::iterator last, bool superscript)
90 // nothing to get here
94 // is this a scriptinset?
95 if (!(*pos)->asScriptInset())
98 // do we want superscripts only?
99 if (superscript && !(*pos)->asScriptInset()->hasUp())
102 // it is a scriptinset, use it.
109 // try to extract an "argument" to some function.
110 // returns position behind the argument
111 MathData::iterator extractArgument(MathData & ar,
112 MathData::iterator pos, MathData::iterator last,
113 ExternalMath kind, bool function = false)
115 // nothing to get here
119 // something delimited _is_ an argument
120 if ((*pos)->asDelimInset()) {
121 // leave out delimiters if this is a function argument
122 // unless we are doing MathML, in which case we do want
124 if (function && kind != MATHML && kind != HTML) {
125 MathData const & arg = (*pos)->asDelimInset()->cell(0);
126 MathData::const_iterator cur = arg.begin();
127 MathData::const_iterator end = arg.end();
129 ar.push_back(*cur++);
135 // if there's one, get following superscript only if this
136 // isn't a function argument
138 extractScript(ar, pos, last, true);
142 // always take the first thing, no matter what it is
145 // go ahead if possible
150 // if the next item is a super/subscript, it most certainly belongs
151 // to the thing we have
152 extractScript(ar, pos, last, false);
156 // but it might be more than that.
157 // FIXME: not implemented
158 //for (MathData::iterator it = pos + 1; it != last; ++it) {
159 // // always take the first thing, no matter
161 // ar.push_back(*it);
169 // returns sequence of char with same code starting at it up to end
170 // it might be less, though...
171 docstring charSequence
172 (MathData::const_iterator it, MathData::const_iterator end)
175 for (; it != end && (*it)->asCharInset(); ++it)
176 s += (*it)->getChar();
181 void extractStrings(MathData & ar)
183 //lyxerr << "\nStrings from: " << ar << endl;
184 for (size_t i = 0; i < ar.size(); ++i) {
185 if (!ar[i]->asCharInset())
187 docstring s = charSequence(ar.begin() + i, ar.end());
188 ar[i] = MathAtom(new InsetMathString(s));
189 ar.erase(i + 1, i + s.size());
191 //lyxerr << "\nStrings to: " << ar << endl;
195 void extractMatrices(MathData & ar)
197 //lyxerr << "\nMatrices from: " << ar << endl;
198 // first pass for explicitly delimited stuff
199 for (size_t i = 0; i < ar.size(); ++i) {
200 InsetMathDelim const * const inset = ar[i]->asDelimInset();
203 MathData const & arr = inset->cell(0);
206 if (!arr.front()->asGridInset())
208 ar[i] = MathAtom(new InsetMathMatrix(*(arr.front()->asGridInset()),
209 inset->left_, inset->right_));
212 // second pass for AMS "pmatrix" etc
213 for (size_t i = 0; i < ar.size(); ++i) {
214 InsetMathAMSArray const * const inset = ar[i]->asAMSArrayInset();
216 string left = inset->name_left();
219 string right = inset->name_right();
222 ar[i] = MathAtom(new InsetMathMatrix(*inset, from_ascii(left), from_ascii(right)));
225 //lyxerr << "\nMatrices to: " << ar << endl;
229 // convert this inset somehow to a string
230 bool extractString(MathAtom const & at, docstring & str)
233 str = docstring(1, at->getChar());
236 if (at->asStringInset()) {
237 str = at->asStringInset()->str();
244 // is this a known function?
245 bool isKnownFunction(docstring const & str)
247 for (int i = 0; function_names[i]; ++i) {
248 if (str == function_names[i])
255 // extract a function name from this inset
256 bool extractFunctionName(MathAtom const & at, docstring & str)
258 if (at->asSymbolInset()) {
259 str = at->asSymbolInset()->name();
260 return isKnownFunction(str);
262 if (at->asUnknownInset()) {
263 // assume it is well known...
267 if (at->asFontInset() && at->name() == "mathrm") {
268 // assume it is well known...
269 MathData const & ar = at->asFontInset()->cell(0);
270 str = charSequence(ar.begin(), ar.end());
271 return ar.size() == str.size();
277 bool testString(MathAtom const & at, docstring const & str)
280 return extractString(at, s) && str == s;
284 bool testString(MathAtom const & at, char const * const str)
286 return testString(at, from_ascii(str));
289 // search end of nested sequence
290 MathData::iterator endNestSearch(
291 MathData::iterator it,
292 MathData::iterator last,
293 TestItemFunc testOpen,
294 TestItemFunc testClose
297 for (int level = 0; it != last; ++it) {
309 // replace nested sequences by a real Insets
312 TestItemFunc testOpen,
313 TestItemFunc testClose,
314 ReplaceArgumentFunc replaceArg)
316 Buffer * buf = ar.buffer();
317 // use indices rather than iterators for the loop because we are going
318 // to modify the array.
319 for (size_t i = 0; i < ar.size(); ++i) {
320 // check whether this is the begin of the sequence
321 if (!testOpen(ar[i]))
324 // search end of sequence
325 MathData::iterator it = ar.begin() + i;
326 MathData::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
330 // replace the original stuff by the new inset
331 ar[i] = replaceArg(MathData(buf, it + 1, jt));
332 ar.erase(it + 1, jt + 1);
339 // split scripts into seperate super- and subscript insets. sub goes in
343 void splitScripts(MathData & ar)
345 Buffer * buf = ar.buffer();
346 //lyxerr << "\nScripts from: " << ar << endl;
347 for (size_t i = 0; i < ar.size(); ++i) {
348 InsetMathScript const * script = ar[i]->asScriptInset();
350 // is this a script inset and do we also have a superscript?
351 if (!script || !script->hasUp())
354 // we must have a nucleus if we only have a superscript
355 if (!script->hasDown() && script->nuc().size() == 0)
358 if (script->nuc().size() == 1) {
359 // leave alone sums and integrals
360 InsetMathSymbol const * sym =
361 script->nuc().front()->asSymbolInset();
362 if (sym && (sym->name() == "sum" || sym->name() == "int"))
366 // create extra script inset and move superscript over
367 InsetMathScript * p = ar[i].nucleus()->asScriptInset();
368 auto_ptr<InsetMathScript> q(new InsetMathScript(buf, true));
369 swap(q->up(), p->up());
370 p->removeScript(true);
372 // if we don't have a subscript, get rid of the ScriptInset
373 if (!script->hasDown()) {
374 MathData arg(p->nuc());
375 MathData::const_iterator it = arg.begin();
376 MathData::const_iterator et = arg.end();
379 ar.insert(i++, *it++);
383 // insert new inset behind
384 ar.insert(i, MathAtom(q.release()));
386 //lyxerr << "\nScripts to: " << ar << endl;
394 void extractExps(MathData & ar)
396 Buffer * buf = ar.buffer();
397 //lyxerr << "\nExps from: " << ar << endl;
398 for (size_t i = 0; i + 1 < ar.size(); ++i) {
400 if (ar[i]->getChar() != 'e')
403 // we need an exponent but no subscript
404 InsetMathScript const * sup = ar[i + 1]->asScriptInset();
405 if (!sup || sup->hasDown())
408 // create a proper exp-inset as replacement
409 ar[i] = MathAtom(new InsetMathExFunc(buf, from_ascii("exp"), sup->cell(1)));
412 //lyxerr << "\nExps to: " << ar << endl;
417 // extract det(...) from |matrix|
419 void extractDets(MathData & ar)
421 Buffer * buf = ar.buffer();
422 //lyxerr << "\ndet from: " << ar << endl;
423 for (MathData::iterator it = ar.begin(); it != ar.end(); ++it) {
424 InsetMathDelim const * del = (*it)->asDelimInset();
429 *it = MathAtom(new InsetMathExFunc(buf, from_ascii("det"), del->cell(0)));
431 //lyxerr << "\ndet to: " << ar << endl;
439 bool isDigitOrSimilar(char_type c)
441 return ('0' <= c && c <= '9') || c == '.';
445 // returns sequence of digits
446 docstring digitSequence
447 (MathData::const_iterator it, MathData::const_iterator end)
450 for (; it != end && (*it)->asCharInset(); ++it) {
451 if (!isDigitOrSimilar((*it)->getChar()))
453 s += (*it)->getChar();
459 void extractNumbers(MathData & ar)
461 //lyxerr << "\nNumbers from: " << ar << endl;
462 for (size_t i = 0; i < ar.size(); ++i) {
463 if (!ar[i]->asCharInset())
465 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
468 docstring s = digitSequence(ar.begin() + i, ar.end());
470 ar[i] = MathAtom(new InsetMathNumber(s));
471 ar.erase(i + 1, i + s.size());
473 //lyxerr << "\nNumbers to: " << ar << endl;
482 bool testOpenParen(MathAtom const & at)
484 return testString(at, "(");
488 bool testCloseParen(MathAtom const & at)
490 return testString(at, ")");
494 MathAtom replaceParenDelims(const MathData & ar)
496 return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
497 from_ascii("("), from_ascii(")"), ar));
501 bool testOpenBracket(MathAtom const & at)
503 return testString(at, "[");
507 bool testCloseBracket(MathAtom const & at)
509 return testString(at, "]");
513 MathAtom replaceBracketDelims(const MathData & ar)
515 return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
516 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, ExternalMath kind)
540 // we need at least two items...
544 Buffer * buf = ar.buffer();
546 //lyxerr << "\nFunctions from: " << ar << endl;
547 for (size_t i = 0; i + 1 < ar.size(); ++i) {
548 MathData::iterator it = ar.begin() + i;
549 MathData::iterator jt = it + 1;
553 // it certainly is if it is well known...
554 if (!extractFunctionName(*it, name)) {
555 // is this a user defined function?
556 // it it probably not, if it doesn't have a name.
557 if (!extractString(*it, name))
559 // it is not if it has no argument
562 // guess so, if this is followed by
563 // a DelimInset with a single item in the cell
564 InsetMathDelim const * del = (*jt)->asDelimInset();
565 if (!del || del->cell(0).size() != 1)
567 // fall trough into main branch
570 // do we have an exponent like in
571 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
573 extractScript(exp, jt, ar.end(), true);
575 // create a proper inset as replacement
576 auto_ptr<InsetMathExFunc> p(new InsetMathExFunc(buf, name));
578 // jt points to the "argument". Get hold of this.
579 MathData::iterator st =
580 extractArgument(p->cell(0), jt, ar.end(), kind, true);
582 // replace the function name by a real function inset
583 *it = MathAtom(p.release());
585 // remove the source of the argument from the array
586 ar.erase(it + 1, st);
588 // re-insert exponent
589 ar.insert(i + 1, exp);
590 //lyxerr << "\nFunctions to: " << ar << endl;
599 bool testSymbol(MathAtom const & at, docstring const & name)
601 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
605 bool testSymbol(MathAtom const & at, char const * const name)
607 return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
611 bool testIntSymbol(MathAtom const & at)
613 return testSymbol(at, from_ascii("int"));
617 bool testIntegral(MathAtom const & at)
621 ( at->asScriptInset()
622 && at->asScriptInset()->nuc().size()
623 && testIntSymbol(at->asScriptInset()->nuc().back()) );
628 bool testIntDiff(MathAtom const & at)
630 return testString(at, "d");
634 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
635 // assume 'extractDelims' ran before
636 void extractIntegrals(MathData & ar, ExternalMath kind)
638 // we need at least three items...
642 Buffer * buf = ar.buffer();
644 //lyxerr << "\nIntegrals from: " << ar << endl;
645 for (size_t i = 0; i + 1 < ar.size(); ++i) {
646 MathData::iterator it = ar.begin() + i;
649 MathData::iterator jt =
650 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
652 // something sensible found?
656 // is this a integral name?
657 if (!testIntegral(*it))
660 // core ist part from behind the scripts to the 'd'
661 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("int")));
663 // handle scripts if available
664 if (!testIntSymbol(*it)) {
665 p->cell(2) = (*it)->asScriptInset()->down();
666 p->cell(3) = (*it)->asScriptInset()->up();
668 p->cell(0) = MathData(buf, it + 1, jt);
670 // use the "thing" behind the 'd' as differential
671 MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end(), kind);
674 ar.erase(it + 1, tt);
675 *it = MathAtom(p.release());
677 //lyxerr << "\nIntegrals to: " << ar << endl;
681 bool testTermDelimiter(MathAtom const & at)
683 return testString(at, "+") || testString(at, "-");
687 // try to extract a "term", i.e., something delimited by '+' or '-'.
688 // returns position behind the term
689 MathData::iterator extractTerm(MathData & ar,
690 MathData::iterator pos, MathData::iterator last)
692 while (pos != last && !testTermDelimiter(*pos)) {
705 bool testEqualSign(MathAtom const & at)
707 return testString(at, "=");
711 bool testSumSymbol(MathAtom const & p)
713 return testSymbol(p, from_ascii("sum"));
717 bool testSum(MathAtom const & at)
721 ( at->asScriptInset()
722 && at->asScriptInset()->nuc().size()
723 && testSumSymbol(at->asScriptInset()->nuc().back()) );
727 // replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
728 // assume 'extractDelims' ran before
729 void extractSums(MathData & ar)
731 // we need at least two items...
735 Buffer * buf = ar.buffer();
737 //lyxerr << "\nSums from: " << ar << endl;
738 for (size_t i = 0; i + 1 < ar.size(); ++i) {
739 MathData::iterator it = ar.begin() + i;
741 // is this a sum name?
745 // create a proper inset as replacement
746 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("sum")));
748 // collect lower bound and summation index
749 InsetMathScript const * sub = ar[i]->asScriptInset();
750 if (sub && sub->hasDown()) {
751 // try to figure out the summation index from the subscript
752 MathData const & ar = sub->down();
753 MathData::const_iterator xt =
754 find_if(ar.begin(), ar.end(), &testEqualSign);
755 if (xt != ar.end()) {
756 // we found a '=', use everything in front of that as index,
757 // and everything behind as lower index
758 p->cell(1) = MathData(buf, ar.begin(), xt);
759 p->cell(2) = MathData(buf, xt + 1, ar.end());
761 // use everything as summation index, don't use scripts.
766 // collect upper bound
767 if (sub && sub->hasUp())
768 p->cell(3) = sub->up();
770 // use something behind the script as core
771 MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
774 ar.erase(it + 1, tt);
775 *it = MathAtom(p.release());
777 //lyxerr << "\nSums to: " << ar << endl;
782 // search differential stuff
785 // tests for 'd' or '\partial'
786 bool testDiffItem(MathAtom const & at)
788 if (testString(at, "d") || testSymbol(at, "partial"))
791 // we may have d^n .../d and splitScripts() has not yet seen it
792 InsetMathScript const * sup = at->asScriptInset();
793 if (sup && !sup->hasDown() && sup->hasUp() && sup->nuc().size() == 1) {
794 MathAtom const & ma = sup->nuc().front();
795 return testString(ma, "d") || testSymbol(ma, "partial");
801 bool testDiffArray(MathData const & ar)
803 return ar.size() && testDiffItem(ar.front());
807 bool testDiffFrac(MathAtom const & at)
811 && testDiffArray(at->asFracInset()->cell(0))
812 && testDiffArray(at->asFracInset()->cell(1));
816 void extractDiff(MathData & ar)
818 Buffer * buf = ar.buffer();
819 //lyxerr << "\nDiffs from: " << ar << endl;
820 for (size_t i = 0; i < ar.size(); ++i) {
821 MathData::iterator it = ar.begin() + i;
823 // is this a "differential fraction"?
824 if (!testDiffFrac(*it))
827 InsetMathFrac const * f = (*it)->asFracInset();
829 lyxerr << "should not happen" << endl;
833 // create a proper diff inset
834 auto_ptr<InsetMathDiff> diff(new InsetMathDiff(buf));
836 // collect function, let jt point behind last used item
837 MathData::iterator jt = it + 1;
839 MathData numer(f->cell(0));
841 if (numer.size() > 1 && numer[1]->asScriptInset()) {
842 // this is something like d^n f(x) / d... or d^n / d...
845 if (numer.size() > 2)
846 diff->cell(0) = MathData(buf, numer.begin() + 2, numer.end());
848 jt = extractTerm(diff->cell(0), jt, ar.end());
850 // simply d f(x) / d... or d/d...
851 if (numer.size() > 1)
852 diff->cell(0) = MathData(buf, numer.begin() + 1, numer.end());
854 jt = extractTerm(diff->cell(0), jt, ar.end());
857 // collect denominator parts
858 MathData denom(f->cell(1));
860 for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
862 MathData::iterator et
863 = find_if(dt + 1, denom.end(), &testDiffItem);
866 MathData::iterator st = et - 1;
867 InsetMathScript const * script = (*st)->asScriptInset();
868 if (script && script->hasUp()) {
869 // things like d.../dx^n
871 if (extractNumber(script->up(), mult)) {
872 //lyxerr << "mult: " << mult << endl;
873 for (int i = 0; i < mult; ++i)
874 diff->addDer(MathData(buf, dt + 1, st));
878 diff->addDer(MathData(buf, dt + 1, et));
884 ar.erase(it + 1, jt);
885 *it = MathAtom(diff.release());
887 //lyxerr << "\nDiffs to: " << ar << endl;
896 bool testRightArrow(MathAtom const & at)
898 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
903 // replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
904 // assume 'extractDelims' ran before
905 void extractLims(MathData & ar)
907 Buffer * buf = ar.buffer();
908 //lyxerr << "\nLimits from: " << ar << endl;
909 for (size_t i = 0; i < ar.size(); ++i) {
910 MathData::iterator it = ar.begin() + i;
912 // must be a script inset with a subscript (without superscript)
913 InsetMathScript const * sub = (*it)->asScriptInset();
914 if (!sub || !sub->hasDown() || sub->hasUp() || sub->nuc().size() != 1)
917 // is this a limit function?
918 if (!testSymbol(sub->nuc().front(), "lim"))
921 // subscript must contain a -> symbol
922 MathData const & s = sub->down();
923 MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
927 // the -> splits the subscript int x and x0
928 MathData x = MathData(buf, s.begin(), st);
929 MathData x0 = MathData(buf, st + 1, s.end());
931 // use something behind the script as core
933 MathData::iterator tt = extractTerm(f, it + 1, ar.end());
936 ar.erase(it + 1, tt);
938 // create a proper inset as replacement
939 *it = MathAtom(new InsetMathLim(buf, f, x, x0));
941 //lyxerr << "\nLimits to: " << ar << endl;
949 void extractStructure(MathData & ar, ExternalMath kind)
951 //lyxerr << "\nStructure from: " << ar << endl;
952 if (kind != MATHML && kind != HTML)
955 extractIntegrals(ar, kind);
956 if (kind != MATHML && kind != HTML)
960 extractFunctions(ar, kind);
961 if (kind != MATHML && kind != HTML) {
968 //lyxerr << "\nStructure to: " << ar << endl;
974 string captureOutput(string const & cmd, string const & data)
976 // In order to avoid parsing problems with command interpreters
977 // we pass input data through a file
978 FileName const cas_tmpfile = FileName::tempName("casinput");
979 if (cas_tmpfile.empty()) {
980 lyxerr << "Warning: cannot create temporary file."
984 ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
987 string command = cmd + " < "
988 + quoteName(cas_tmpfile.toFilesystemEncoding());
989 lyxerr << "calling: " << cmd
990 << "\ninput: '" << data << "'" << endl;
991 cmd_ret const ret = runCommand(command);
992 cas_tmpfile.removeFile();
996 size_t get_matching_brace(string const & str, size_t i)
999 size_t n = str.size();
1001 i = str.find_first_of("{}", i+1);
1014 size_t get_matching_brace_back(string const & str, size_t i)
1018 i = str.find_last_of("{}", i-1);
1031 MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1033 odocstringstream os;
1034 MaximaStream ms(os);
1036 docstring expr = os.str();
1037 docstring const header = from_ascii("simpsum:true;");
1040 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1041 // try to fix missing '*' the hard way
1043 // > echo "2x;" | maxima
1045 // (C1) Incorrect syntax: x is not an infix operator
1049 lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1050 docstring full = header + "tex(" + expr + ");";
1051 out = captureOutput("maxima", to_utf8(full));
1053 // leave loop if expression syntax is probably ok
1054 if (out.find("Incorrect syntax") == npos)
1057 // search line with "Incorrect syntax"
1058 istringstream is(out);
1062 if (line.find("Incorrect syntax") != npos)
1066 // 2nd next line is the one with caret
1069 size_t pos = line.find('^');
1070 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1071 if (pos == npos || pos < 4)
1072 break; // caret position not found
1073 pos -= 4; // skip the "tex(" part
1074 if (expr[pos] == '*')
1075 break; // two '*' in a row are definitely bad
1076 expr.insert(pos, from_ascii("*"));
1079 vector<string> tmp = getVectorFromString(out, "$$");
1083 out = subst(tmp[1], "\\>", string());
1084 lyxerr << "output: '" << out << "'" << endl;
1086 // Ugly code that tries to make the result prettier
1087 size_t i = out.find("\\mathchoice");
1089 size_t j = get_matching_brace(out, i + 12);
1090 size_t k = get_matching_brace(out, j + 1);
1091 k = get_matching_brace(out, k + 1);
1092 k = get_matching_brace(out, k + 1);
1093 string mid = out.substr(i + 13, j - i - 13);
1094 if (mid.find("\\over") != npos)
1095 mid = '{' + mid + '}';
1096 out = out.substr(0, i)
1098 + out.substr(k + 1);
1099 //lyxerr << "output: " << out << endl;
1100 i = out.find("\\mathchoice", i);
1104 i = out.find("\\over");
1106 size_t j = get_matching_brace_back(out, i - 1);
1107 if (j == npos || j == 0)
1109 size_t k = get_matching_brace(out, i + 5);
1110 if (k == npos || k + 1 == out.size())
1112 out = out.substr(0, j - 1)
1114 + out.substr(j, i - j)
1115 + out.substr(i + 5, k - i - 4)
1116 + out.substr(k + 2);
1117 //lyxerr << "output: " << out << endl;
1118 i = out.find("\\over", i + 4);
1121 mathed_parse_cell(res, from_utf8(out));
1126 MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1128 string header = "readlib(latex):\n";
1130 // remove the \\it for variable names
1131 //"#`latex/csname_font` := `\\it `:"
1133 "`latex/csname_font` := ``:\n";
1135 // export matrices in (...) instead of [...]
1137 "`latex/latex/matrix` := "
1138 "subs(`[`=`(`, `]`=`)`,"
1139 "eval(`latex/latex/matrix`)):\n";
1141 // replace \\cdots with proper '*'
1143 "`latex/latex/*` := "
1144 "subs(`\\,`=`\\cdot `,"
1145 "eval(`latex/latex/*`)):\n";
1147 // remove spurious \\noalign{\\medskip} in matrix output
1149 "`latex/latex/matrix`:= "
1150 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1151 "eval(`latex/latex/matrix`)):\n";
1153 //"#`latex/latex/symbol` "
1154 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1156 string trailer = "quit;";
1157 odocstringstream os;
1160 string expr = to_utf8(os.str());
1161 lyxerr << "ar: '" << ar << "'\n"
1162 << "ms: '" << expr << "'" << endl;
1164 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1165 // try to fix missing '*' the hard way by using mint
1167 // ... > echo "1A;" | mint -i 1 -S -s -q
1170 // Probably missing an operator such as * p
1172 lyxerr << "checking expr: '" << expr << "'" << endl;
1173 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1175 break; // expression syntax is ok
1176 istringstream is(out);
1179 if (line.find("on line") != 0)
1180 break; // error message not identified
1182 size_t pos = line.find('^');
1183 if (pos == string::npos || pos < 15)
1184 break; // caret position not found
1185 pos -= 15; // skip the "on line ..." part
1186 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1187 break; // two '*' in a row are definitely bad
1188 expr.insert(pos, 1, '*');
1191 // FIXME UNICODE Is utf8 encoding correct?
1192 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1193 string out = captureOutput("maple -q", header + full + trailer);
1199 mathed_parse_cell(res, from_utf8(out));
1204 MathData pipeThroughOctave(docstring const &, MathData const & ar)
1206 odocstringstream os;
1207 OctaveStream vs(os);
1209 string expr = to_utf8(os.str());
1212 Buffer * buf = const_cast<Buffer *>(ar.buffer());
1213 lyxerr << "pipe: ar: '" << ar << "'\n"
1214 << "pipe: expr: '" << expr << "'" << endl;
1216 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1218 // try to fix missing '*' the hard way
1220 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1223 lyxerr << "checking expr: '" << expr << "'" << endl;
1224 out = captureOutput("octave -q 2>&1", expr);
1225 lyxerr << "output: '" << out << "'" << endl;
1227 // leave loop if expression syntax is probably ok
1228 if (out.find("parse error:") == string::npos)
1231 // search line with single caret
1232 istringstream is(out);
1236 lyxerr << "skipping line: '" << line << "'" << endl;
1237 if (line.find(">>> ") != string::npos)
1241 // found line with error, next line is the one with caret
1243 size_t pos = line.find('^');
1244 lyxerr << "caret line: '" << line << "'" << endl;
1245 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1246 if (pos == string::npos || pos < 4)
1247 break; // caret position not found
1248 pos -= 4; // skip the ">>> " part
1249 if (expr[pos] == '*')
1250 break; // two '*' in a row are definitely bad
1251 expr.insert(pos, 1, '*');
1254 // remove 'ans = ' taking into account that there may be an
1255 // ansi control sequence before, such as '\033[?1034hans = '
1256 size_t i = out.find("ans = ");
1257 if (i == string::npos)
1259 out = out.substr(i + 6);
1261 // parse output as matrix or single number
1262 MathAtom at(new InsetMathArray(buf, from_ascii("array"), from_utf8(out)));
1263 InsetMathArray const * mat = at->asArrayInset();
1265 if (mat->ncols() == 1 && mat->nrows() == 1)
1266 res.append(mat->cell(0));
1268 res.push_back(MathAtom(
1269 new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
1270 res.back().nucleus()->cell(0).push_back(at);
1276 string fromMathematicaName(string const & name)
1278 if (name == "Sin") return "sin";
1279 if (name == "Sinh") return "sinh";
1280 if (name == "ArcSin") return "arcsin";
1281 if (name == "Cos") return "cos";
1282 if (name == "Cosh") return "cosh";
1283 if (name == "ArcCos") return "arccos";
1284 if (name == "Tan") return "tan";
1285 if (name == "Tanh") return "tanh";
1286 if (name == "ArcTan") return "arctan";
1287 if (name == "Cot") return "cot";
1288 if (name == "Coth") return "coth";
1289 if (name == "Csc") return "csc";
1290 if (name == "Sec") return "sec";
1291 if (name == "Exp") return "exp";
1292 if (name == "Log") return "log";
1293 if (name == "Arg" ) return "arg";
1294 if (name == "Det" ) return "det";
1295 if (name == "GCD" ) return "gcd";
1296 if (name == "Max" ) return "max";
1297 if (name == "Min" ) return "min";
1298 if (name == "Erf" ) return "erf";
1299 if (name == "Erfc" ) return "erfc";
1304 void prettifyMathematicaOutput(string & out, string const & macroName,
1305 bool roman, bool translate)
1307 string const macro = "\\" + macroName + "{";
1308 size_t const len = macro.length();
1309 size_t i = out.find(macro);
1312 size_t const j = get_matching_brace(out, i + len);
1313 string const name = out.substr(i + len, j - i - len);
1314 out = out.substr(0, i)
1315 + (roman ? "\\mathrm{" : "")
1316 + (translate ? fromMathematicaName(name) : name)
1317 + out.substr(roman ? j : j + 1);
1318 //lyxerr << "output: " << out << endl;
1319 i = out.find(macro, i);
1324 MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1326 odocstringstream os;
1327 MathematicaStream ms(os);
1329 // FIXME UNICODE Is utf8 encoding correct?
1330 string const expr = to_utf8(os.str());
1333 lyxerr << "expr: '" << expr << "'" << endl;
1335 string const full = "TeXForm[" + expr + "]";
1336 out = captureOutput("math", full);
1337 lyxerr << "output: '" << out << "'" << endl;
1339 size_t pos1 = out.find("Out[1]//TeXForm= ");
1340 size_t pos2 = out.find("In[2]:=");
1342 if (pos1 == string::npos || pos2 == string::npos)
1345 // get everything from pos1+17 to pos2
1346 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1347 out = subst(subst(out, '\r', ' '), '\n', ' ');
1349 // tries to make the result prettier
1350 prettifyMathematicaOutput(out, "Mfunction", true, true);
1351 prettifyMathematicaOutput(out, "Muserfunction", true, false);
1352 prettifyMathematicaOutput(out, "Mvariable", false, false);
1355 mathed_parse_cell(res, from_utf8(out));
1363 void write(MathData const & dat, WriteStream & wi)
1367 wi.firstitem() = true;
1368 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it) {
1370 wi.firstitem() = false;
1375 void normalize(MathData const & ar, NormalStream & os)
1377 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1378 (*it)->normalize(os);
1382 void octave(MathData const & dat, OctaveStream & os)
1385 extractStructure(ar, OCTAVE);
1386 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1391 void maple(MathData const & dat, MapleStream & os)
1394 extractStructure(ar, MAPLE);
1395 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1400 void maxima(MathData const & dat, MaximaStream & os)
1403 extractStructure(ar, MAXIMA);
1404 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1409 void mathematica(MathData const & dat, MathematicaStream & os)
1412 extractStructure(ar, MATHEMATICA);
1413 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1414 (*it)->mathematica(os);
1418 void mathmlize(MathData const & dat, MathStream & os)
1421 extractStructure(ar, MATHML);
1424 else if (ar.size() == 1)
1428 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1429 (*it)->mathmlize(os);
1435 void htmlize(MathData const & dat, HtmlStream & os)
1438 extractStructure(ar, HTML);
1441 if (ar.size() == 1) {
1445 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1450 // convert this inset somehow to a number
1451 bool extractNumber(MathData const & ar, int & i)
1453 idocstringstream is(charSequence(ar.begin(), ar.end()));
1459 bool extractNumber(MathData const & ar, double & d)
1461 idocstringstream is(charSequence(ar.begin(), ar.end()));
1467 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1468 MathData const & ar)
1470 if (lang == "octave")
1471 return pipeThroughOctave(extra, ar);
1473 if (lang == "maxima")
1474 return pipeThroughMaxima(extra, ar);
1476 if (lang == "maple")
1477 return pipeThroughMaple(extra, ar);
1479 if (lang == "mathematica")
1480 return pipeThroughMathematica(extra, ar);
1482 // create normalized expression
1483 odocstringstream os;
1484 NormalStream ns(os);
1485 os << '[' << extra << ' ';
1488 // FIXME UNICODE Is utf8 encoding correct?
1489 string data = to_utf8(os.str());
1491 // search external script
1492 FileName const file = libFileSearch("mathed", "extern_" + lang);
1494 lyxerr << "converter to '" << lang << "' not found" << endl;
1498 // run external sript
1499 string out = captureOutput(file.absFileName(), data);
1501 mathed_parse_cell(res, from_utf8(out));