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"
40 #include "support/debug.h"
41 #include "support/docstream.h"
42 #include "support/FileName.h"
43 #include "support/filetools.h"
44 #include "support/gettext.h"
45 #include "support/lstrings.h"
46 #include "support/TempFile.h"
47 #include "support/textutils.h"
55 using namespace lyx::support;
71 static char const * function_names[] = {
72 "arccos", "arcsin", "arctan", "arg", "bmod",
73 "cos", "cosh", "cot", "coth", "csc", "deg",
74 "det", "dim", "exp", "gcd", "hom", "inf", "ker",
75 "lg", "lim", "liminf", "limsup", "ln", "log",
76 "max", "min", "sec", "sin", "sinh", "sup",
77 "tan", "tanh", "Pr", 0
80 static size_t const npos = lyx::docstring::npos;
82 // define a function for tests
83 typedef bool TestItemFunc(MathAtom const &);
85 // define a function for replacing subexpressions
86 typedef MathAtom ReplaceArgumentFunc(const MathData & ar);
90 // try to extract a super/subscript
91 // modify iterator position to point behind the thing
92 bool extractScript(MathData & ar,
93 MathData::iterator & pos, MathData::iterator last, bool superscript)
95 // nothing to get here
99 // is this a scriptinset?
100 if (!(*pos)->asScriptInset())
103 // do we want superscripts only?
104 if (superscript && !(*pos)->asScriptInset()->hasUp())
107 // it is a scriptinset, use it.
114 // try to extract an "argument" to some function.
115 // returns position behind the argument
116 MathData::iterator extractArgument(MathData & ar,
117 MathData::iterator pos, MathData::iterator last,
118 ExternalMath kind, bool function = false)
120 // nothing to get here
124 // something delimited _is_ an argument
125 if ((*pos)->asDelimInset()) {
126 // leave out delimiters if this is a function argument
127 // unless we are doing MathML, in which case we do want
129 if (function && kind != MATHML && kind != HTML) {
130 MathData const & arg = (*pos)->asDelimInset()->cell(0);
131 MathData::const_iterator cur = arg.begin();
132 MathData::const_iterator end = arg.end();
134 ar.push_back(*cur++);
140 // if there's one, get following superscript only if this
141 // isn't a function argument
143 extractScript(ar, pos, last, true);
147 // always take the first thing, no matter what it is
150 // go ahead if possible
155 // if the next item is a super/subscript, it most certainly belongs
156 // to the thing we have
157 extractScript(ar, pos, last, false);
161 // but it might be more than that.
162 // FIXME: not implemented
163 //for (MathData::iterator it = pos + 1; it != last; ++it) {
164 // // always take the first thing, no matter
166 // ar.push_back(*it);
174 // returns sequence of char with same code starting at it up to end
175 // it might be less, though...
176 docstring charSequence
177 (MathData::const_iterator it, MathData::const_iterator end)
180 for (; it != end && (*it)->asCharInset(); ++it)
181 s += (*it)->getChar();
186 void extractStrings(MathData & ar)
188 //lyxerr << "\nStrings from: " << ar << endl;
189 for (size_t i = 0; i < ar.size(); ++i) {
190 if (!ar[i]->asCharInset())
192 docstring s = charSequence(ar.begin() + i, ar.end());
193 ar[i] = MathAtom(new InsetMathString(s));
194 ar.erase(i + 1, i + s.size());
196 //lyxerr << "\nStrings to: " << ar << endl;
200 void extractMatrices(MathData & ar)
202 //lyxerr << "\nMatrices from: " << ar << endl;
203 // first pass for explicitly delimited stuff
204 for (size_t i = 0; i < ar.size(); ++i) {
205 InsetMathDelim const * const inset = ar[i]->asDelimInset();
208 MathData const & arr = inset->cell(0);
211 if (!arr.front()->asGridInset())
213 ar[i] = MathAtom(new InsetMathMatrix(*(arr.front()->asGridInset()),
214 inset->left_, inset->right_));
217 // second pass for AMS "pmatrix" etc
218 for (size_t i = 0; i < ar.size(); ++i) {
219 InsetMathAMSArray const * const inset = ar[i]->asAMSArrayInset();
221 string left = inset->name_left();
224 string right = inset->name_right();
227 ar[i] = MathAtom(new InsetMathMatrix(*inset, from_ascii(left), from_ascii(right)));
230 //lyxerr << "\nMatrices to: " << ar << endl;
234 // convert this inset somehow to a string
235 bool extractString(MathAtom const & at, docstring & str)
238 str = docstring(1, at->getChar());
241 if (at->asStringInset()) {
242 str = at->asStringInset()->str();
249 // is this a known function?
250 bool isKnownFunction(docstring const & str)
252 for (int i = 0; function_names[i]; ++i) {
253 if (str == function_names[i])
260 // extract a function name from this inset
261 bool extractFunctionName(MathAtom const & at, docstring & str)
263 if (at->asSymbolInset()) {
264 str = at->asSymbolInset()->name();
265 return isKnownFunction(str);
267 if (at->asUnknownInset()) {
268 // assume it is well known...
272 if (at->asFontInset() && at->name() == "mathrm") {
273 // assume it is well known...
274 MathData const & ar = at->asFontInset()->cell(0);
275 str = charSequence(ar.begin(), ar.end());
276 return ar.size() == str.size();
282 bool testString(MathAtom const & at, docstring const & str)
285 return extractString(at, s) && str == s;
289 bool testString(MathAtom const & at, char const * const str)
291 return testString(at, from_ascii(str));
294 // search end of nested sequence
295 MathData::iterator endNestSearch(
296 MathData::iterator it,
297 MathData::iterator last,
298 TestItemFunc testOpen,
299 TestItemFunc testClose
302 for (int level = 0; it != last; ++it) {
314 // replace nested sequences by a real Insets
317 TestItemFunc testOpen,
318 TestItemFunc testClose,
319 ReplaceArgumentFunc replaceArg)
321 Buffer * buf = ar.buffer();
322 // use indices rather than iterators for the loop because we are going
323 // to modify the array.
324 for (size_t i = 0; i < ar.size(); ++i) {
325 // check whether this is the begin of the sequence
326 if (!testOpen(ar[i]))
329 // search end of sequence
330 MathData::iterator it = ar.begin() + i;
331 MathData::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
335 // replace the original stuff by the new inset
336 ar[i] = replaceArg(MathData(buf, it + 1, jt));
337 ar.erase(it + 1, jt + 1);
344 // split scripts into seperate super- and subscript insets. sub goes in
348 void splitScripts(MathData & ar)
350 Buffer * buf = ar.buffer();
351 //lyxerr << "\nScripts from: " << ar << endl;
352 for (size_t i = 0; i < ar.size(); ++i) {
353 InsetMathScript const * script = ar[i]->asScriptInset();
355 // is this a script inset and do we also have a superscript?
356 if (!script || !script->hasUp())
359 // we must have a nucleus if we only have a superscript
360 if (!script->hasDown() && script->nuc().empty())
363 if (script->nuc().size() == 1) {
364 // leave alone sums and integrals
365 InsetMathSymbol const * sym =
366 script->nuc().front()->asSymbolInset();
367 if (sym && (sym->name() == "sum" || sym->name() == "int"))
371 // create extra script inset and move superscript over
372 InsetMathScript * p = ar[i].nucleus()->asScriptInset();
373 auto_ptr<InsetMathScript> q(new InsetMathScript(buf, true));
374 swap(q->up(), p->up());
375 p->removeScript(true);
377 // if we don't have a subscript, get rid of the ScriptInset
378 if (!script->hasDown()) {
379 MathData arg(p->nuc());
380 MathData::const_iterator it = arg.begin();
381 MathData::const_iterator et = arg.end();
384 ar.insert(i++, *it++);
388 // insert new inset behind
389 ar.insert(i, MathAtom(q.release()));
391 //lyxerr << "\nScripts to: " << ar << endl;
399 void extractExps(MathData & ar)
401 Buffer * buf = ar.buffer();
402 //lyxerr << "\nExps from: " << ar << endl;
403 for (size_t i = 0; i + 1 < ar.size(); ++i) {
405 if (ar[i]->getChar() != 'e')
408 // we need an exponent but no subscript
409 InsetMathScript const * sup = ar[i + 1]->asScriptInset();
410 if (!sup || sup->hasDown())
413 // create a proper exp-inset as replacement
414 ar[i] = MathAtom(new InsetMathExFunc(buf, from_ascii("exp"), sup->cell(1)));
417 //lyxerr << "\nExps to: " << ar << endl;
422 // extract det(...) from |matrix|
424 void extractDets(MathData & ar)
426 Buffer * buf = ar.buffer();
427 //lyxerr << "\ndet from: " << ar << endl;
428 for (MathData::iterator it = ar.begin(); it != ar.end(); ++it) {
429 InsetMathDelim const * del = (*it)->asDelimInset();
434 *it = MathAtom(new InsetMathExFunc(buf, from_ascii("det"), del->cell(0)));
436 //lyxerr << "\ndet to: " << ar << endl;
444 bool isDigitOrSimilar(char_type c)
446 return ('0' <= c && c <= '9') || c == '.';
450 // returns sequence of digits
451 docstring digitSequence
452 (MathData::const_iterator it, MathData::const_iterator end)
455 for (; it != end && (*it)->asCharInset(); ++it) {
456 if (!isDigitOrSimilar((*it)->getChar()))
458 s += (*it)->getChar();
464 void extractNumbers(MathData & ar)
466 //lyxerr << "\nNumbers from: " << ar << endl;
467 for (size_t i = 0; i < ar.size(); ++i) {
468 if (!ar[i]->asCharInset())
470 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
473 docstring s = digitSequence(ar.begin() + i, ar.end());
475 ar[i] = MathAtom(new InsetMathNumber(s));
476 ar.erase(i + 1, i + s.size());
478 //lyxerr << "\nNumbers to: " << ar << endl;
487 bool testOpenParen(MathAtom const & at)
489 return testString(at, "(");
493 bool testCloseParen(MathAtom const & at)
495 return testString(at, ")");
499 MathAtom replaceParenDelims(const MathData & ar)
501 return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
502 from_ascii("("), from_ascii(")"), ar));
506 bool testOpenBracket(MathAtom const & at)
508 return testString(at, "[");
512 bool testCloseBracket(MathAtom const & at)
514 return testString(at, "]");
518 MathAtom replaceBracketDelims(const MathData & ar)
520 return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
521 from_ascii("["), from_ascii("]"), ar));
525 // replace '('...')' and '['...']' sequences by a real InsetMathDelim
526 void extractDelims(MathData & ar)
528 //lyxerr << "\nDelims from: " << ar << endl;
529 replaceNested(ar, testOpenParen, testCloseParen, replaceParenDelims);
530 replaceNested(ar, testOpenBracket, testCloseBracket, replaceBracketDelims);
531 //lyxerr << "\nDelims to: " << ar << endl;
537 // search well-known functions
541 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real InsetMathExFunc
542 // assume 'extractDelims' ran before
543 void extractFunctions(MathData & ar, ExternalMath kind)
545 // FIXME From what I can see, this is quite broken right now, for reasons
546 // I will note below. (RGH)
548 // we need at least two items...
552 Buffer * buf = ar.buffer();
554 //lyxerr << "\nFunctions from: " << ar << endl;
555 for (size_t i = 0; i + 1 < ar.size(); ++i) {
556 MathData::iterator it = ar.begin() + i;
557 MathData::iterator jt = it + 1;
561 // it certainly is if it is well known...
563 // FIXME This will never give us anything. When we get here, *it will
564 // never point at a string, but only at a character. I.e., if we are
565 // working on "sin(x)", then we are seeing:
566 // [char s mathalpha][char i mathalpha][char n mathalpha][delim ( ) [char x mathalpha]]
567 // and of course we will not find the function name "sin" in there, but
570 // It appears that we original ran extractStrings() before we ran
571 // extractFunctions(), but Andre changed this at f200be55, I think
572 // because this messed up what he was trying to do with "dx" in the
573 // context of integrals.
575 // This could be fixed by looking at a charSequence instead of just at
576 // the various characters, one by one. But I am not sure I understand
577 // exactly what we are trying to do here. And it involves a lot of
579 if (!extractFunctionName(*it, name)) {
580 // is this a user defined function?
581 // probably not, if it doesn't have a name.
582 if (!extractString(*it, name))
584 // it is not if it has no argument
587 // guess so, if this is followed by
588 // a DelimInset with a single item in the cell
589 InsetMathDelim const * del = (*jt)->asDelimInset();
590 if (!del || del->cell(0).size() != 1)
592 // fall through into main branch
595 // do we have an exponent like in
596 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
598 extractScript(exp, jt, ar.end(), true);
600 // create a proper inset as replacement
601 auto_ptr<InsetMathExFunc> p(new InsetMathExFunc(buf, name));
603 // jt points to the "argument". Get hold of this.
604 MathData::iterator st =
605 extractArgument(p->cell(0), jt, ar.end(), kind, true);
607 // replace the function name by a real function inset
608 *it = MathAtom(p.release());
610 // remove the source of the argument from the array
611 ar.erase(it + 1, st);
613 // re-insert exponent
614 ar.insert(i + 1, exp);
615 //lyxerr << "\nFunctions to: " << ar << endl;
624 bool testSymbol(MathAtom const & at, docstring const & name)
626 return at->asSymbolInset() && at->asSymbolInset()->name() == name;
630 bool testSymbol(MathAtom const & at, char const * const name)
632 return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
636 bool testIntSymbol(MathAtom const & at)
638 return testSymbol(at, from_ascii("int"));
642 bool testIntegral(MathAtom const & at)
646 ( at->asScriptInset()
647 && !at->asScriptInset()->nuc().empty()
648 && testIntSymbol(at->asScriptInset()->nuc().back()) );
653 bool testIntDiff(MathAtom const & at)
655 return testString(at, "d");
659 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
660 // assume 'extractDelims' ran before
661 void extractIntegrals(MathData & ar, ExternalMath kind)
663 // we need at least three items...
667 Buffer * buf = ar.buffer();
669 //lyxerr << "\nIntegrals from: " << ar << endl;
670 for (size_t i = 0; i + 1 < ar.size(); ++i) {
671 MathData::iterator it = ar.begin() + i;
674 MathData::iterator jt =
675 endNestSearch(it, ar.end(), testIntegral, testIntDiff);
677 // something sensible found?
681 // is this a integral name?
682 if (!testIntegral(*it))
685 // core ist part from behind the scripts to the 'd'
686 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("int")));
688 // handle scripts if available
689 if (!testIntSymbol(*it)) {
690 p->cell(2) = (*it)->asScriptInset()->down();
691 p->cell(3) = (*it)->asScriptInset()->up();
693 p->cell(0) = MathData(buf, it + 1, jt);
695 // use the "thing" behind the 'd' as differential
696 MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end(), kind);
699 ar.erase(it + 1, tt);
700 *it = MathAtom(p.release());
702 //lyxerr << "\nIntegrals to: " << ar << endl;
706 bool testTermDelimiter(MathAtom const & at)
708 return testString(at, "+") || testString(at, "-");
712 // try to extract a "term", i.e., something delimited by '+' or '-'.
713 // returns position behind the term
714 MathData::iterator extractTerm(MathData & ar,
715 MathData::iterator pos, MathData::iterator last)
717 while (pos != last && !testTermDelimiter(*pos)) {
730 bool testEqualSign(MathAtom const & at)
732 return testString(at, "=");
736 bool testSumSymbol(MathAtom const & p)
738 return testSymbol(p, from_ascii("sum"));
742 bool testSum(MathAtom const & at)
746 ( at->asScriptInset()
747 && !at->asScriptInset()->nuc().empty()
748 && testSumSymbol(at->asScriptInset()->nuc().back()) );
752 // replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
753 // assume 'extractDelims' ran before
754 void extractSums(MathData & ar)
756 // we need at least two items...
760 Buffer * buf = ar.buffer();
762 //lyxerr << "\nSums from: " << ar << endl;
763 for (size_t i = 0; i + 1 < ar.size(); ++i) {
764 MathData::iterator it = ar.begin() + i;
766 // is this a sum name?
770 // create a proper inset as replacement
771 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("sum")));
773 // collect lower bound and summation index
774 InsetMathScript const * sub = ar[i]->asScriptInset();
775 if (sub && sub->hasDown()) {
776 // try to figure out the summation index from the subscript
777 MathData const & ar = sub->down();
778 MathData::const_iterator xt =
779 find_if(ar.begin(), ar.end(), &testEqualSign);
780 if (xt != ar.end()) {
781 // we found a '=', use everything in front of that as index,
782 // and everything behind as lower index
783 p->cell(1) = MathData(buf, ar.begin(), xt);
784 p->cell(2) = MathData(buf, xt + 1, ar.end());
786 // use everything as summation index, don't use scripts.
791 // collect upper bound
792 if (sub && sub->hasUp())
793 p->cell(3) = sub->up();
795 // use something behind the script as core
796 MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
799 ar.erase(it + 1, tt);
800 *it = MathAtom(p.release());
802 //lyxerr << "\nSums to: " << ar << endl;
807 // search differential stuff
810 // tests for 'd' or '\partial'
811 bool testDiffItem(MathAtom const & at)
813 if (testString(at, "d") || testSymbol(at, "partial"))
816 // we may have d^n .../d and splitScripts() has not yet seen it
817 InsetMathScript const * sup = at->asScriptInset();
818 if (sup && !sup->hasDown() && sup->hasUp() && sup->nuc().size() == 1) {
819 MathAtom const & ma = sup->nuc().front();
820 return testString(ma, "d") || testSymbol(ma, "partial");
826 bool testDiffArray(MathData const & ar)
828 return !ar.empty() && testDiffItem(ar.front());
832 bool testDiffFrac(MathAtom const & at)
836 && testDiffArray(at->asFracInset()->cell(0))
837 && testDiffArray(at->asFracInset()->cell(1));
841 void extractDiff(MathData & ar)
843 Buffer * buf = ar.buffer();
844 //lyxerr << "\nDiffs from: " << ar << endl;
845 for (size_t i = 0; i < ar.size(); ++i) {
846 MathData::iterator it = ar.begin() + i;
848 // is this a "differential fraction"?
849 if (!testDiffFrac(*it))
852 InsetMathFrac const * f = (*it)->asFracInset();
854 lyxerr << "should not happen" << endl;
858 // create a proper diff inset
859 auto_ptr<InsetMathDiff> diff(new InsetMathDiff(buf));
861 // collect function, let jt point behind last used item
862 MathData::iterator jt = it + 1;
864 MathData numer(f->cell(0));
866 if (numer.size() > 1 && numer[1]->asScriptInset()) {
867 // this is something like d^n f(x) / d... or d^n / d...
870 if (numer.size() > 2)
871 diff->cell(0) = MathData(buf, numer.begin() + 2, numer.end());
873 jt = extractTerm(diff->cell(0), jt, ar.end());
875 // simply d f(x) / d... or d/d...
876 if (numer.size() > 1)
877 diff->cell(0) = MathData(buf, numer.begin() + 1, numer.end());
879 jt = extractTerm(diff->cell(0), jt, ar.end());
882 // collect denominator parts
883 MathData denom(f->cell(1));
885 for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
887 MathData::iterator et
888 = find_if(dt + 1, denom.end(), &testDiffItem);
891 MathData::iterator st = et - 1;
892 InsetMathScript const * script = (*st)->asScriptInset();
893 if (script && script->hasUp()) {
894 // things like d.../dx^n
896 if (extractNumber(script->up(), mult)) {
897 //lyxerr << "mult: " << mult << endl;
898 for (int i = 0; i < mult; ++i)
899 diff->addDer(MathData(buf, dt + 1, st));
903 diff->addDer(MathData(buf, dt + 1, et));
909 ar.erase(it + 1, jt);
910 *it = MathAtom(diff.release());
912 //lyxerr << "\nDiffs to: " << ar << endl;
921 bool testRightArrow(MathAtom const & at)
923 return testSymbol(at, "to") || testSymbol(at, "rightarrow");
928 // replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
929 // assume 'extractDelims' ran before
930 void extractLims(MathData & ar)
932 Buffer * buf = ar.buffer();
933 //lyxerr << "\nLimits from: " << ar << endl;
934 for (size_t i = 0; i < ar.size(); ++i) {
935 MathData::iterator it = ar.begin() + i;
937 // must be a script inset with a subscript (without superscript)
938 InsetMathScript const * sub = (*it)->asScriptInset();
939 if (!sub || !sub->hasDown() || sub->hasUp() || sub->nuc().size() != 1)
942 // is this a limit function?
943 if (!testSymbol(sub->nuc().front(), "lim"))
946 // subscript must contain a -> symbol
947 MathData const & s = sub->down();
948 MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
952 // the -> splits the subscript int x and x0
953 MathData x = MathData(buf, s.begin(), st);
954 MathData x0 = MathData(buf, st + 1, s.end());
956 // use something behind the script as core
958 MathData::iterator tt = extractTerm(f, it + 1, ar.end());
961 ar.erase(it + 1, tt);
963 // create a proper inset as replacement
964 *it = MathAtom(new InsetMathLim(buf, f, x, x0));
966 //lyxerr << "\nLimits to: " << ar << endl;
974 void extractStructure(MathData & ar, ExternalMath kind)
976 //lyxerr << "\nStructure from: " << ar << endl;
977 if (kind != MATHML && kind != HTML)
980 extractIntegrals(ar, kind);
981 if (kind != MATHML && kind != HTML)
985 if (kind != MATHML && kind != HTML) {
986 extractFunctions(ar, kind);
993 //lyxerr << "\nStructure to: " << ar << endl;
999 string captureOutput(string const & cmd, string const & data)
1001 // In order to avoid parsing problems with command interpreters
1002 // we pass input data through a file
1003 TempFile tempfile("casinput");
1004 FileName const cas_tmpfile = tempfile.name();
1005 if (cas_tmpfile.empty()) {
1006 lyxerr << "Warning: cannot create temporary file."
1010 ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
1013 string command = cmd + " < "
1014 + quoteName(cas_tmpfile.toFilesystemEncoding());
1015 lyxerr << "calling: " << cmd
1016 << "\ninput: '" << data << "'" << endl;
1017 cmd_ret const ret = runCommand(command);
1021 size_t get_matching_brace(string const & str, size_t i)
1024 size_t n = str.size();
1026 i = str.find_first_of("{}", i+1);
1039 size_t get_matching_brace_back(string const & str, size_t i)
1043 i = str.find_last_of("{}", i-1);
1056 MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1058 odocstringstream os;
1059 MaximaStream ms(os);
1061 docstring expr = os.str();
1062 docstring const header = from_ascii("simpsum:true;");
1065 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1066 // try to fix missing '*' the hard way
1068 // > echo "2x;" | maxima
1070 // (C1) Incorrect syntax: x is not an infix operator
1074 lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1075 docstring full = header + "tex(" + expr + ");";
1076 out = captureOutput("maxima", to_utf8(full));
1078 // leave loop if expression syntax is probably ok
1079 if (out.find("Incorrect syntax") == npos)
1082 // search line with "Incorrect syntax"
1083 istringstream is(out);
1087 if (line.find("Incorrect syntax") != npos)
1091 // 2nd next line is the one with caret
1094 size_t pos = line.find('^');
1095 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1096 if (pos == npos || pos < 4)
1097 break; // caret position not found
1098 pos -= 4; // skip the "tex(" part
1099 if (expr[pos] == '*')
1100 break; // two '*' in a row are definitely bad
1101 expr.insert(pos, from_ascii("*"));
1104 vector<string> tmp = getVectorFromString(out, "$$");
1108 out = subst(tmp[1], "\\>", string());
1109 lyxerr << "output: '" << out << "'" << endl;
1111 // Ugly code that tries to make the result prettier
1112 size_t i = out.find("\\mathchoice");
1114 size_t j = get_matching_brace(out, i + 12);
1115 size_t k = get_matching_brace(out, j + 1);
1116 k = get_matching_brace(out, k + 1);
1117 k = get_matching_brace(out, k + 1);
1118 string mid = out.substr(i + 13, j - i - 13);
1119 if (mid.find("\\over") != npos)
1120 mid = '{' + mid + '}';
1121 out = out.substr(0, i)
1123 + out.substr(k + 1);
1124 //lyxerr << "output: " << out << endl;
1125 i = out.find("\\mathchoice", i);
1128 i = out.find("\\over");
1130 size_t j = get_matching_brace_back(out, i - 1);
1131 if (j == npos || j == 0)
1133 size_t k = get_matching_brace(out, i + 5);
1134 if (k == npos || k + 1 == out.size())
1136 out = out.substr(0, j - 1)
1138 + out.substr(j, i - j)
1139 + out.substr(i + 5, k - i - 4)
1140 + out.substr(k + 2);
1141 //lyxerr << "output: " << out << endl;
1142 i = out.find("\\over", i + 4);
1145 mathed_parse_cell(res, from_utf8(out));
1150 MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1152 string header = "readlib(latex):\n";
1154 // remove the \\it for variable names
1155 //"#`latex/csname_font` := `\\it `:"
1157 "`latex/csname_font` := ``:\n";
1159 // export matrices in (...) instead of [...]
1161 "`latex/latex/matrix` := "
1162 "subs(`[`=`(`, `]`=`)`,"
1163 "eval(`latex/latex/matrix`)):\n";
1165 // replace \\cdots with proper '*'
1167 "`latex/latex/*` := "
1168 "subs(`\\,`=`\\cdot `,"
1169 "eval(`latex/latex/*`)):\n";
1171 // remove spurious \\noalign{\\medskip} in matrix output
1173 "`latex/latex/matrix`:= "
1174 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1175 "eval(`latex/latex/matrix`)):\n";
1177 //"#`latex/latex/symbol` "
1178 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1180 string trailer = "quit;";
1181 odocstringstream os;
1184 string expr = to_utf8(os.str());
1185 lyxerr << "ar: '" << ar << "'\n"
1186 << "ms: '" << expr << "'" << endl;
1188 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1189 // try to fix missing '*' the hard way by using mint
1191 // ... > echo "1A;" | mint -i 1 -S -s -q
1194 // Probably missing an operator such as * p
1196 lyxerr << "checking expr: '" << expr << "'" << endl;
1197 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1199 break; // expression syntax is ok
1200 istringstream is(out);
1203 if (!prefixIs(line, "on line"))
1204 break; // error message not identified
1206 size_t pos = line.find('^');
1207 if (pos == string::npos || pos < 15)
1208 break; // caret position not found
1209 pos -= 15; // skip the "on line ..." part
1210 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1211 break; // two '*' in a row are definitely bad
1212 expr.insert(pos, 1, '*');
1215 // FIXME UNICODE Is utf8 encoding correct?
1216 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1217 string out = captureOutput("maple -q", header + full + trailer);
1223 mathed_parse_cell(res, from_utf8(out));
1228 MathData pipeThroughOctave(docstring const &, MathData const & ar)
1230 odocstringstream os;
1231 OctaveStream vs(os);
1233 string expr = to_utf8(os.str());
1236 Buffer * buf = const_cast<Buffer *>(ar.buffer());
1237 lyxerr << "pipe: ar: '" << ar << "'\n"
1238 << "pipe: expr: '" << expr << "'" << endl;
1240 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1242 // try to fix missing '*' the hard way
1244 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1247 lyxerr << "checking expr: '" << expr << "'" << endl;
1248 out = captureOutput("octave -q 2>&1", expr);
1249 lyxerr << "output: '" << out << "'" << endl;
1251 // leave loop if expression syntax is probably ok
1252 if (out.find("parse error:") == string::npos)
1255 // search line with single caret
1256 istringstream is(out);
1260 lyxerr << "skipping line: '" << line << "'" << endl;
1261 if (line.find(">>> ") != string::npos)
1265 // found line with error, next line is the one with caret
1267 size_t pos = line.find('^');
1268 lyxerr << "caret line: '" << line << "'" << endl;
1269 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1270 if (pos == string::npos || pos < 4)
1271 break; // caret position not found
1272 pos -= 4; // skip the ">>> " part
1273 if (expr[pos] == '*')
1274 break; // two '*' in a row are definitely bad
1275 expr.insert(pos, 1, '*');
1278 // remove 'ans = ' taking into account that there may be an
1279 // ansi control sequence before, such as '\033[?1034hans = '
1280 size_t i = out.find("ans = ");
1281 if (i == string::npos)
1283 out = out.substr(i + 6);
1285 // parse output as matrix or single number
1286 MathAtom at(new InsetMathArray(buf, from_ascii("array"), from_utf8(out)));
1287 InsetMathArray const * mat = at->asArrayInset();
1289 if (mat->ncols() == 1 && mat->nrows() == 1)
1290 res.append(mat->cell(0));
1292 res.push_back(MathAtom(
1293 new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
1294 res.back().nucleus()->cell(0).push_back(at);
1300 string fromMathematicaName(string const & name)
1302 if (name == "Sin") return "sin";
1303 if (name == "Sinh") return "sinh";
1304 if (name == "ArcSin") return "arcsin";
1305 if (name == "Cos") return "cos";
1306 if (name == "Cosh") return "cosh";
1307 if (name == "ArcCos") return "arccos";
1308 if (name == "Tan") return "tan";
1309 if (name == "Tanh") return "tanh";
1310 if (name == "ArcTan") return "arctan";
1311 if (name == "Cot") return "cot";
1312 if (name == "Coth") return "coth";
1313 if (name == "Csc") return "csc";
1314 if (name == "Sec") return "sec";
1315 if (name == "Exp") return "exp";
1316 if (name == "Log") return "log";
1317 if (name == "Arg" ) return "arg";
1318 if (name == "Det" ) return "det";
1319 if (name == "GCD" ) return "gcd";
1320 if (name == "Max" ) return "max";
1321 if (name == "Min" ) return "min";
1322 if (name == "Erf" ) return "erf";
1323 if (name == "Erfc" ) return "erfc";
1328 void prettifyMathematicaOutput(string & out, string const & macroName,
1329 bool roman, bool translate)
1331 string const macro = "\\" + macroName + "{";
1332 size_t const len = macro.length();
1333 size_t i = out.find(macro);
1336 size_t const j = get_matching_brace(out, i + len);
1337 string const name = out.substr(i + len, j - i - len);
1338 out = out.substr(0, i)
1339 + (roman ? "\\mathrm{" : "")
1340 + (translate ? fromMathematicaName(name) : name)
1341 + out.substr(roman ? j : j + 1);
1342 //lyxerr << "output: " << out << endl;
1343 i = out.find(macro, i);
1348 MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1350 odocstringstream os;
1351 MathematicaStream ms(os);
1353 // FIXME UNICODE Is utf8 encoding correct?
1354 string const expr = to_utf8(os.str());
1357 lyxerr << "expr: '" << expr << "'" << endl;
1359 string const full = "TeXForm[" + expr + "]";
1360 out = captureOutput("math", full);
1361 lyxerr << "output: '" << out << "'" << endl;
1363 size_t pos1 = out.find("Out[1]//TeXForm= ");
1364 size_t pos2 = out.find("In[2]:=");
1366 if (pos1 == string::npos || pos2 == string::npos)
1369 // get everything from pos1+17 to pos2
1370 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1371 out = subst(subst(out, '\r', ' '), '\n', ' ');
1373 // tries to make the result prettier
1374 prettifyMathematicaOutput(out, "Mfunction", true, true);
1375 prettifyMathematicaOutput(out, "Muserfunction", true, false);
1376 prettifyMathematicaOutput(out, "Mvariable", false, false);
1379 mathed_parse_cell(res, from_utf8(out));
1387 void write(MathData const & dat, WriteStream & wi)
1389 wi.firstitem() = true;
1391 for (MathData::const_iterator it = dat.begin(); it != dat.end(); ++it) {
1392 InsetMathChar const * const c = (*it)->asCharInset();
1401 wi.firstitem() = false;
1406 wi.firstitem() = false;
1411 void writeString(docstring const & s, WriteStream & os)
1413 if (!os.latex() || os.lockedMode()) {
1414 os << (os.asciiOnly() ? escape(s) : s);
1418 docstring::const_iterator cit = s.begin();
1419 docstring::const_iterator end = s.end();
1421 // We may already be inside an \ensuremath command.
1422 bool in_forced_mode = os.pendingBrace();
1424 // We will take care of matching braces.
1425 os.pendingBrace(false);
1427 while (cit != end) {
1428 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
1429 char_type const c = *cit;
1430 docstring command(1, c);
1432 bool termination = false;
1434 Encodings::latexMathChar(c, mathmode, os.encoding(), command, termination)) {
1435 if (os.textMode()) {
1436 if (in_forced_mode) {
1437 // we were inside \lyxmathsym
1440 in_forced_mode = false;
1442 if (!isASCII(c) && os.textMode()) {
1443 os << "\\ensuremath{";
1445 in_forced_mode = true;
1447 } else if (isASCII(c) && in_forced_mode) {
1448 // we were inside \ensuremath
1451 in_forced_mode = false;
1453 } else if (!os.textMode()) {
1454 if (in_forced_mode) {
1455 // we were inside \ensuremath
1457 in_forced_mode = false;
1459 os << "\\lyxmathsym{";
1460 in_forced_mode = true;
1465 // We may need a space if the command contains a macro
1466 // and the last char is ASCII.
1468 os.pendingSpace(true);
1469 } catch (EncodingException const & e) {
1470 switch (os.output()) {
1471 case WriteStream::wsDryrun: {
1472 os << "<" << _("LyX Warning: ")
1473 << _("uncodable character") << " '";
1474 os << docstring(1, e.failed_char);
1478 case WriteStream::wsPreview: {
1479 // indicate the encoding error by a boxed '?'
1480 os << "{\\fboxsep=1pt\\fbox{?}}";
1481 LYXERR0("Uncodable character" << " '"
1482 << docstring(1, e.failed_char)
1486 case WriteStream::wsDefault:
1495 if (in_forced_mode && os.textMode()) {
1496 // We have to care for closing \lyxmathsym
1500 os.pendingBrace(in_forced_mode);
1505 void normalize(MathData const & ar, NormalStream & os)
1507 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1508 (*it)->normalize(os);
1512 void octave(MathData const & dat, OctaveStream & os)
1515 extractStructure(ar, OCTAVE);
1516 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1521 void maple(MathData const & dat, MapleStream & os)
1524 extractStructure(ar, MAPLE);
1525 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1530 void maxima(MathData const & dat, MaximaStream & os)
1533 extractStructure(ar, MAXIMA);
1534 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1539 void mathematica(MathData const & dat, MathematicaStream & os)
1542 extractStructure(ar, MATHEMATICA);
1543 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1544 (*it)->mathematica(os);
1548 void mathmlize(MathData const & dat, MathStream & os)
1551 extractStructure(ar, MATHML);
1554 else if (ar.size() == 1)
1558 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1559 (*it)->mathmlize(os);
1565 void htmlize(MathData const & dat, HtmlStream & os)
1568 extractStructure(ar, HTML);
1571 if (ar.size() == 1) {
1575 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1580 // convert this inset somehow to a number
1581 bool extractNumber(MathData const & ar, int & i)
1583 idocstringstream is(charSequence(ar.begin(), ar.end()));
1585 // Do not convert is implicitly to bool, since that is forbidden in C++11.
1590 bool extractNumber(MathData const & ar, double & d)
1592 idocstringstream is(charSequence(ar.begin(), ar.end()));
1594 // Do not convert is implicitly to bool, since that is forbidden in C++11.
1599 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1600 MathData const & ar)
1602 if (lang == "octave")
1603 return pipeThroughOctave(extra, ar);
1605 if (lang == "maxima")
1606 return pipeThroughMaxima(extra, ar);
1608 if (lang == "maple")
1609 return pipeThroughMaple(extra, ar);
1611 if (lang == "mathematica")
1612 return pipeThroughMathematica(extra, ar);
1614 // create normalized expression
1615 odocstringstream os;
1616 NormalStream ns(os);
1617 os << '[' << extra << ' ';
1620 // FIXME UNICODE Is utf8 encoding correct?
1621 string data = to_utf8(os.str());
1623 // search external script
1624 FileName const file = libFileSearch("mathed", "extern_" + lang);
1626 lyxerr << "converter to '" << lang << "' not found" << endl;
1630 // run external sript
1631 string out = captureOutput(file.absFileName(), data);
1633 mathed_parse_cell(res, from_utf8(out));