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().empty())
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 // FIXME From what I can see, this is quite broken right now, for reasons
541 // I will note below. (RGH)
543 // we need at least two items...
547 Buffer * buf = ar.buffer();
549 //lyxerr << "\nFunctions from: " << ar << endl;
550 for (size_t i = 0; i + 1 < ar.size(); ++i) {
551 MathData::iterator it = ar.begin() + i;
552 MathData::iterator jt = it + 1;
556 // it certainly is if it is well known...
558 // FIXME This will never give us anything. When we get here, *it will
559 // never point at a string, but only at a character. I.e., if we are
560 // working on "sin(x)", then we are seeing:
561 // [char s mathalpha][char i mathalpha][char n mathalpha][delim ( ) [char x mathalpha]]
562 // and of course we will not find the function name "sin" in there, but
565 // It appears that we original ran extractStrings() before we ran
566 // extractFunctions(), but Andre changed this at f200be55, I think
567 // because this messed up what he was trying to do with "dx" in the
568 // context of integrals.
570 // This could be fixed by looking at a charSequence instead of just at
571 // the various characters, one by one. But I am not sure I understand
572 // exactly what we are trying to do here. And it involves a lot of
574 if (!extractFunctionName(*it, name)) {
575 // is this a user defined function?
576 // probably not, if it doesn't have a name.
577 if (!extractString(*it, name))
579 // it is not if it has no argument
582 // guess so, if this is followed by
583 // a DelimInset with a single item in the cell
584 InsetMathDelim const * del = (*jt)->asDelimInset();
585 if (!del || del->cell(0).size() != 1)
587 // fall through into main branch
590 // do we have an exponent like in
591 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
593 extractScript(exp, jt, ar.end(), true);
595 // create a proper inset as replacement
596 auto_ptr<InsetMathExFunc> p(new InsetMathExFunc(buf, name));
598 // jt points to the "argument". Get hold of this.
599 MathData::iterator st =
600 extractArgument(p->cell(0), jt, ar.end(), kind, true);
602 // replace the function name by a real function inset
603 *it = MathAtom(p.release());
605 // remove the source of the argument from the array
606 ar.erase(it + 1, st);
608 // re-insert exponent
609 ar.insert(i + 1, exp);
610 //lyxerr << "\nFunctions to: " << ar << endl;
619 bool testSymbol(MathAtom const & at, docstring const & name)
621 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
625 bool testSymbol(MathAtom const & at, char const * const name)
627 return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
631 bool testIntSymbol(MathAtom const & at)
633 return testSymbol(at, from_ascii("int"));
637 bool testIntegral(MathAtom const & at)
641 ( at->asScriptInset()
642 && !at->asScriptInset()->nuc().empty()
643 && testIntSymbol(at->asScriptInset()->nuc().back()) );
648 bool testIntDiff(MathAtom const & at)
650 return testString(at, "d");
654 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
655 // assume 'extractDelims' ran before
656 void extractIntegrals(MathData & ar, ExternalMath kind)
658 // we need at least three items...
662 Buffer * buf = ar.buffer();
664 //lyxerr << "\nIntegrals from: " << ar << endl;
665 for (size_t i = 0; i + 1 < ar.size(); ++i) {
666 MathData::iterator it = ar.begin() + i;
669 MathData::iterator jt =
670 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
672 // something sensible found?
676 // is this a integral name?
677 if (!testIntegral(*it))
680 // core ist part from behind the scripts to the 'd'
681 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("int")));
683 // handle scripts if available
684 if (!testIntSymbol(*it)) {
685 p->cell(2) = (*it)->asScriptInset()->down();
686 p->cell(3) = (*it)->asScriptInset()->up();
688 p->cell(0) = MathData(buf, it + 1, jt);
690 // use the "thing" behind the 'd' as differential
691 MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end(), kind);
694 ar.erase(it + 1, tt);
695 *it = MathAtom(p.release());
697 //lyxerr << "\nIntegrals to: " << ar << endl;
701 bool testTermDelimiter(MathAtom const & at)
703 return testString(at, "+") || testString(at, "-");
707 // try to extract a "term", i.e., something delimited by '+' or '-'.
708 // returns position behind the term
709 MathData::iterator extractTerm(MathData & ar,
710 MathData::iterator pos, MathData::iterator last)
712 while (pos != last && !testTermDelimiter(*pos)) {
725 bool testEqualSign(MathAtom const & at)
727 return testString(at, "=");
731 bool testSumSymbol(MathAtom const & p)
733 return testSymbol(p, from_ascii("sum"));
737 bool testSum(MathAtom const & at)
741 ( at->asScriptInset()
742 && !at->asScriptInset()->nuc().empty()
743 && testSumSymbol(at->asScriptInset()->nuc().back()) );
747 // replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
748 // assume 'extractDelims' ran before
749 void extractSums(MathData & ar)
751 // we need at least two items...
755 Buffer * buf = ar.buffer();
757 //lyxerr << "\nSums from: " << ar << endl;
758 for (size_t i = 0; i + 1 < ar.size(); ++i) {
759 MathData::iterator it = ar.begin() + i;
761 // is this a sum name?
765 // create a proper inset as replacement
766 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("sum")));
768 // collect lower bound and summation index
769 InsetMathScript const * sub = ar[i]->asScriptInset();
770 if (sub && sub->hasDown()) {
771 // try to figure out the summation index from the subscript
772 MathData const & ar = sub->down();
773 MathData::const_iterator xt =
774 find_if(ar.begin(), ar.end(), &testEqualSign);
775 if (xt != ar.end()) {
776 // we found a '=', use everything in front of that as index,
777 // and everything behind as lower index
778 p->cell(1) = MathData(buf, ar.begin(), xt);
779 p->cell(2) = MathData(buf, xt + 1, ar.end());
781 // use everything as summation index, don't use scripts.
786 // collect upper bound
787 if (sub && sub->hasUp())
788 p->cell(3) = sub->up();
790 // use something behind the script as core
791 MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
794 ar.erase(it + 1, tt);
795 *it = MathAtom(p.release());
797 //lyxerr << "\nSums to: " << ar << endl;
802 // search differential stuff
805 // tests for 'd' or '\partial'
806 bool testDiffItem(MathAtom const & at)
808 if (testString(at, "d") || testSymbol(at, "partial"))
811 // we may have d^n .../d and splitScripts() has not yet seen it
812 InsetMathScript const * sup = at->asScriptInset();
813 if (sup && !sup->hasDown() && sup->hasUp() && sup->nuc().size() == 1) {
814 MathAtom const & ma = sup->nuc().front();
815 return testString(ma, "d") || testSymbol(ma, "partial");
821 bool testDiffArray(MathData const & ar)
823 return !ar.empty() && testDiffItem(ar.front());
827 bool testDiffFrac(MathAtom const & at)
831 && testDiffArray(at->asFracInset()->cell(0))
832 && testDiffArray(at->asFracInset()->cell(1));
836 void extractDiff(MathData & ar)
838 Buffer * buf = ar.buffer();
839 //lyxerr << "\nDiffs from: " << ar << endl;
840 for (size_t i = 0; i < ar.size(); ++i) {
841 MathData::iterator it = ar.begin() + i;
843 // is this a "differential fraction"?
844 if (!testDiffFrac(*it))
847 InsetMathFrac const * f = (*it)->asFracInset();
849 lyxerr << "should not happen" << endl;
853 // create a proper diff inset
854 auto_ptr<InsetMathDiff> diff(new InsetMathDiff(buf));
856 // collect function, let jt point behind last used item
857 MathData::iterator jt = it + 1;
859 MathData numer(f->cell(0));
861 if (numer.size() > 1 && numer[1]->asScriptInset()) {
862 // this is something like d^n f(x) / d... or d^n / d...
865 if (numer.size() > 2)
866 diff->cell(0) = MathData(buf, numer.begin() + 2, numer.end());
868 jt = extractTerm(diff->cell(0), jt, ar.end());
870 // simply d f(x) / d... or d/d...
871 if (numer.size() > 1)
872 diff->cell(0) = MathData(buf, numer.begin() + 1, numer.end());
874 jt = extractTerm(diff->cell(0), jt, ar.end());
877 // collect denominator parts
878 MathData denom(f->cell(1));
880 for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
882 MathData::iterator et
883 = find_if(dt + 1, denom.end(), &testDiffItem);
886 MathData::iterator st = et - 1;
887 InsetMathScript const * script = (*st)->asScriptInset();
888 if (script && script->hasUp()) {
889 // things like d.../dx^n
891 if (extractNumber(script->up(), mult)) {
892 //lyxerr << "mult: " << mult << endl;
893 for (int i = 0; i < mult; ++i)
894 diff->addDer(MathData(buf, dt + 1, st));
898 diff->addDer(MathData(buf, dt + 1, et));
904 ar.erase(it + 1, jt);
905 *it = MathAtom(diff.release());
907 //lyxerr << "\nDiffs to: " << ar << endl;
916 bool testRightArrow(MathAtom const & at)
918 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
923 // replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
924 // assume 'extractDelims' ran before
925 void extractLims(MathData & ar)
927 Buffer * buf = ar.buffer();
928 //lyxerr << "\nLimits from: " << ar << endl;
929 for (size_t i = 0; i < ar.size(); ++i) {
930 MathData::iterator it = ar.begin() + i;
932 // must be a script inset with a subscript (without superscript)
933 InsetMathScript const * sub = (*it)->asScriptInset();
934 if (!sub || !sub->hasDown() || sub->hasUp() || sub->nuc().size() != 1)
937 // is this a limit function?
938 if (!testSymbol(sub->nuc().front(), "lim"))
941 // subscript must contain a -> symbol
942 MathData const & s = sub->down();
943 MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
947 // the -> splits the subscript int x and x0
948 MathData x = MathData(buf, s.begin(), st);
949 MathData x0 = MathData(buf, st + 1, s.end());
951 // use something behind the script as core
953 MathData::iterator tt = extractTerm(f, it + 1, ar.end());
956 ar.erase(it + 1, tt);
958 // create a proper inset as replacement
959 *it = MathAtom(new InsetMathLim(buf, f, x, x0));
961 //lyxerr << "\nLimits to: " << ar << endl;
969 void extractStructure(MathData & ar, ExternalMath kind)
971 //lyxerr << "\nStructure from: " << ar << endl;
972 if (kind != MATHML && kind != HTML)
975 extractIntegrals(ar, kind);
976 if (kind != MATHML && kind != HTML)
980 if (kind != MATHML && kind != HTML) {
981 extractFunctions(ar, kind);
988 //lyxerr << "\nStructure to: " << ar << endl;
994 string captureOutput(string const & cmd, string const & data)
996 // In order to avoid parsing problems with command interpreters
997 // we pass input data through a file
998 FileName const cas_tmpfile = FileName::tempName("casinput");
999 if (cas_tmpfile.empty()) {
1000 lyxerr << "Warning: cannot create temporary file."
1004 ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
1007 string command = cmd + " < "
1008 + quoteName(cas_tmpfile.toFilesystemEncoding());
1009 lyxerr << "calling: " << cmd
1010 << "\ninput: '" << data << "'" << endl;
1011 cmd_ret const ret = runCommand(command);
1012 cas_tmpfile.removeFile();
1016 size_t get_matching_brace(string const & str, size_t i)
1019 size_t n = str.size();
1021 i = str.find_first_of("{}", i+1);
1034 size_t get_matching_brace_back(string const & str, size_t i)
1038 i = str.find_last_of("{}", i-1);
1051 MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1053 odocstringstream os;
1054 MaximaStream ms(os);
1056 docstring expr = os.str();
1057 docstring const header = from_ascii("simpsum:true;");
1060 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1061 // try to fix missing '*' the hard way
1063 // > echo "2x;" | maxima
1065 // (C1) Incorrect syntax: x is not an infix operator
1069 lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1070 docstring full = header + "tex(" + expr + ");";
1071 out = captureOutput("maxima", to_utf8(full));
1073 // leave loop if expression syntax is probably ok
1074 if (out.find("Incorrect syntax") == npos)
1077 // search line with "Incorrect syntax"
1078 istringstream is(out);
1082 if (line.find("Incorrect syntax") != npos)
1086 // 2nd next line is the one with caret
1089 size_t pos = line.find('^');
1090 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1091 if (pos == npos || pos < 4)
1092 break; // caret position not found
1093 pos -= 4; // skip the "tex(" part
1094 if (expr[pos] == '*')
1095 break; // two '*' in a row are definitely bad
1096 expr.insert(pos, from_ascii("*"));
1099 vector<string> tmp = getVectorFromString(out, "$$");
1103 out = subst(tmp[1], "\\>", string());
1104 lyxerr << "output: '" << out << "'" << endl;
1106 // Ugly code that tries to make the result prettier
1107 size_t i = out.find("\\mathchoice");
1109 size_t j = get_matching_brace(out, i + 12);
1110 size_t k = get_matching_brace(out, j + 1);
1111 k = get_matching_brace(out, k + 1);
1112 k = get_matching_brace(out, k + 1);
1113 string mid = out.substr(i + 13, j - i - 13);
1114 if (mid.find("\\over") != npos)
1115 mid = '{' + mid + '}';
1116 out = out.substr(0, i)
1118 + out.substr(k + 1);
1119 //lyxerr << "output: " << out << endl;
1120 i = out.find("\\mathchoice", i);
1124 i = out.find("\\over");
1126 size_t j = get_matching_brace_back(out, i - 1);
1127 if (j == npos || j == 0)
1129 size_t k = get_matching_brace(out, i + 5);
1130 if (k == npos || k + 1 == out.size())
1132 out = out.substr(0, j - 1)
1134 + out.substr(j, i - j)
1135 + out.substr(i + 5, k - i - 4)
1136 + out.substr(k + 2);
1137 //lyxerr << "output: " << out << endl;
1138 i = out.find("\\over", i + 4);
1141 mathed_parse_cell(res, from_utf8(out));
1146 MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1148 string header = "readlib(latex):\n";
1150 // remove the \\it for variable names
1151 //"#`latex/csname_font` := `\\it `:"
1153 "`latex/csname_font` := ``:\n";
1155 // export matrices in (...) instead of [...]
1157 "`latex/latex/matrix` := "
1158 "subs(`[`=`(`, `]`=`)`,"
1159 "eval(`latex/latex/matrix`)):\n";
1161 // replace \\cdots with proper '*'
1163 "`latex/latex/*` := "
1164 "subs(`\\,`=`\\cdot `,"
1165 "eval(`latex/latex/*`)):\n";
1167 // remove spurious \\noalign{\\medskip} in matrix output
1169 "`latex/latex/matrix`:= "
1170 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1171 "eval(`latex/latex/matrix`)):\n";
1173 //"#`latex/latex/symbol` "
1174 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1176 string trailer = "quit;";
1177 odocstringstream os;
1180 string expr = to_utf8(os.str());
1181 lyxerr << "ar: '" << ar << "'\n"
1182 << "ms: '" << expr << "'" << endl;
1184 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1185 // try to fix missing '*' the hard way by using mint
1187 // ... > echo "1A;" | mint -i 1 -S -s -q
1190 // Probably missing an operator such as * p
1192 lyxerr << "checking expr: '" << expr << "'" << endl;
1193 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1195 break; // expression syntax is ok
1196 istringstream is(out);
1199 if (line.find("on line") != 0)
1200 break; // error message not identified
1202 size_t pos = line.find('^');
1203 if (pos == string::npos || pos < 15)
1204 break; // caret position not found
1205 pos -= 15; // skip the "on line ..." part
1206 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1207 break; // two '*' in a row are definitely bad
1208 expr.insert(pos, 1, '*');
1211 // FIXME UNICODE Is utf8 encoding correct?
1212 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1213 string out = captureOutput("maple -q", header + full + trailer);
1219 mathed_parse_cell(res, from_utf8(out));
1224 MathData pipeThroughOctave(docstring const &, MathData const & ar)
1226 odocstringstream os;
1227 OctaveStream vs(os);
1229 string expr = to_utf8(os.str());
1232 Buffer * buf = const_cast<Buffer *>(ar.buffer());
1233 lyxerr << "pipe: ar: '" << ar << "'\n"
1234 << "pipe: expr: '" << expr << "'" << endl;
1236 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1238 // try to fix missing '*' the hard way
1240 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1243 lyxerr << "checking expr: '" << expr << "'" << endl;
1244 out = captureOutput("octave -q 2>&1", expr);
1245 lyxerr << "output: '" << out << "'" << endl;
1247 // leave loop if expression syntax is probably ok
1248 if (out.find("parse error:") == string::npos)
1251 // search line with single caret
1252 istringstream is(out);
1256 lyxerr << "skipping line: '" << line << "'" << endl;
1257 if (line.find(">>> ") != string::npos)
1261 // found line with error, next line is the one with caret
1263 size_t pos = line.find('^');
1264 lyxerr << "caret line: '" << line << "'" << endl;
1265 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1266 if (pos == string::npos || pos < 4)
1267 break; // caret position not found
1268 pos -= 4; // skip the ">>> " part
1269 if (expr[pos] == '*')
1270 break; // two '*' in a row are definitely bad
1271 expr.insert(pos, 1, '*');
1274 // remove 'ans = ' taking into account that there may be an
1275 // ansi control sequence before, such as '\033[?1034hans = '
1276 size_t i = out.find("ans = ");
1277 if (i == string::npos)
1279 out = out.substr(i + 6);
1281 // parse output as matrix or single number
1282 MathAtom at(new InsetMathArray(buf, from_ascii("array"), from_utf8(out)));
1283 InsetMathArray const * mat = at->asArrayInset();
1285 if (mat->ncols() == 1 && mat->nrows() == 1)
1286 res.append(mat->cell(0));
1288 res.push_back(MathAtom(
1289 new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
1290 res.back().nucleus()->cell(0).push_back(at);
1296 string fromMathematicaName(string const & name)
1298 if (name == "Sin") return "sin";
1299 if (name == "Sinh") return "sinh";
1300 if (name == "ArcSin") return "arcsin";
1301 if (name == "Cos") return "cos";
1302 if (name == "Cosh") return "cosh";
1303 if (name == "ArcCos") return "arccos";
1304 if (name == "Tan") return "tan";
1305 if (name == "Tanh") return "tanh";
1306 if (name == "ArcTan") return "arctan";
1307 if (name == "Cot") return "cot";
1308 if (name == "Coth") return "coth";
1309 if (name == "Csc") return "csc";
1310 if (name == "Sec") return "sec";
1311 if (name == "Exp") return "exp";
1312 if (name == "Log") return "log";
1313 if (name == "Arg" ) return "arg";
1314 if (name == "Det" ) return "det";
1315 if (name == "GCD" ) return "gcd";
1316 if (name == "Max" ) return "max";
1317 if (name == "Min" ) return "min";
1318 if (name == "Erf" ) return "erf";
1319 if (name == "Erfc" ) return "erfc";
1324 void prettifyMathematicaOutput(string & out, string const & macroName,
1325 bool roman, bool translate)
1327 string const macro = "\\" + macroName + "{";
1328 size_t const len = macro.length();
1329 size_t i = out.find(macro);
1332 size_t const j = get_matching_brace(out, i + len);
1333 string const name = out.substr(i + len, j - i - len);
1334 out = out.substr(0, i)
1335 + (roman ? "\\mathrm{" : "")
1336 + (translate ? fromMathematicaName(name) : name)
1337 + out.substr(roman ? j : j + 1);
1338 //lyxerr << "output: " << out << endl;
1339 i = out.find(macro, i);
1344 MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1346 odocstringstream os;
1347 MathematicaStream ms(os);
1349 // FIXME UNICODE Is utf8 encoding correct?
1350 string const expr = to_utf8(os.str());
1353 lyxerr << "expr: '" << expr << "'" << endl;
1355 string const full = "TeXForm[" + expr + "]";
1356 out = captureOutput("math", full);
1357 lyxerr << "output: '" << out << "'" << endl;
1359 size_t pos1 = out.find("Out[1]//TeXForm= ");
1360 size_t pos2 = out.find("In[2]:=");
1362 if (pos1 == string::npos || pos2 == string::npos)
1365 // get everything from pos1+17 to pos2
1366 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1367 out = subst(subst(out, '\r', ' '), '\n', ' ');
1369 // tries to make the result prettier
1370 prettifyMathematicaOutput(out, "Mfunction", true, true);
1371 prettifyMathematicaOutput(out, "Muserfunction", true, false);
1372 prettifyMathematicaOutput(out, "Mvariable", false, false);
1375 mathed_parse_cell(res, from_utf8(out));
1383 void write(MathData const & dat, WriteStream & wi)
1387 wi.firstitem() = true;
1388 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it) {
1390 wi.firstitem() = false;
1395 void normalize(MathData const & ar, NormalStream & os)
1397 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1398 (*it)->normalize(os);
1402 void octave(MathData const & dat, OctaveStream & os)
1405 extractStructure(ar, OCTAVE);
1406 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1411 void maple(MathData const & dat, MapleStream & os)
1414 extractStructure(ar, MAPLE);
1415 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1420 void maxima(MathData const & dat, MaximaStream & os)
1423 extractStructure(ar, MAXIMA);
1424 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1429 void mathematica(MathData const & dat, MathematicaStream & os)
1432 extractStructure(ar, MATHEMATICA);
1433 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1434 (*it)->mathematica(os);
1438 void mathmlize(MathData const & dat, MathStream & os)
1441 extractStructure(ar, MATHML);
1444 else if (ar.size() == 1)
1448 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1449 (*it)->mathmlize(os);
1455 void htmlize(MathData const & dat, HtmlStream & os)
1458 extractStructure(ar, HTML);
1461 if (ar.size() == 1) {
1465 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1470 // convert this inset somehow to a number
1471 bool extractNumber(MathData const & ar, int & i)
1473 idocstringstream is(charSequence(ar.begin(), ar.end()));
1479 bool extractNumber(MathData const & ar, double & d)
1481 idocstringstream is(charSequence(ar.begin(), ar.end()));
1487 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1488 MathData const & ar)
1490 if (lang == "octave")
1491 return pipeThroughOctave(extra, ar);
1493 if (lang == "maxima")
1494 return pipeThroughMaxima(extra, ar);
1496 if (lang == "maple")
1497 return pipeThroughMaple(extra, ar);
1499 if (lang == "mathematica")
1500 return pipeThroughMathematica(extra, ar);
1502 // create normalized expression
1503 odocstringstream os;
1504 NormalStream ns(os);
1505 os << '[' << extra << ' ';
1508 // FIXME UNICODE Is utf8 encoding correct?
1509 string data = to_utf8(os.str());
1511 // search external script
1512 FileName const file = libFileSearch("mathed", "extern_" + lang);
1514 lyxerr << "converter to '" << lang << "' not found" << endl;
1518 // run external sript
1519 string out = captureOutput(file.absFileName(), data);
1521 mathed_parse_cell(res, from_utf8(out));