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 // Since the CAS is supposed to read the temp file we need
1004 // to unlock it on windows (bug 10262).
1005 auto_ptr<TempFile> tempfile(new TempFile("casinput"));
1006 tempfile->setAutoRemove(false);
1007 FileName const cas_tmpfile = tempfile->name();
1010 if (cas_tmpfile.empty()) {
1011 lyxerr << "Warning: cannot create temporary file."
1015 ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
1018 string command = cmd + " < "
1019 + quoteName(cas_tmpfile.toFilesystemEncoding());
1020 lyxerr << "calling: " << cmd
1021 << "\ninput: '" << data << "'" << endl;
1022 cmd_ret const ret = runCommand(command);
1023 cas_tmpfile.removeFile();
1027 size_t get_matching_brace(string const & str, size_t i)
1030 size_t n = str.size();
1032 i = str.find_first_of("{}", i+1);
1045 size_t get_matching_brace_back(string const & str, size_t i)
1049 i = str.find_last_of("{}", i-1);
1062 MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1064 odocstringstream os;
1065 MaximaStream ms(os);
1067 docstring expr = os.str();
1068 docstring const header = from_ascii("simpsum:true;");
1071 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1072 // try to fix missing '*' the hard way
1074 // > echo "2x;" | maxima
1076 // (C1) Incorrect syntax: x is not an infix operator
1080 lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1081 docstring full = header + "tex(" + expr + ");";
1082 out = captureOutput("maxima", to_utf8(full));
1084 // leave loop if expression syntax is probably ok
1085 if (out.find("Incorrect syntax") == npos)
1088 // search line with "Incorrect syntax"
1089 istringstream is(out);
1093 if (line.find("Incorrect syntax") != npos)
1097 // 2nd next line is the one with caret
1100 size_t pos = line.find('^');
1101 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1102 if (pos == npos || pos < 4)
1103 break; // caret position not found
1104 pos -= 4; // skip the "tex(" part
1105 if (expr[pos] == '*')
1106 break; // two '*' in a row are definitely bad
1107 expr.insert(pos, from_ascii("*"));
1110 vector<string> tmp = getVectorFromString(out, "$$");
1114 out = subst(tmp[1], "\\>", string());
1115 lyxerr << "output: '" << out << "'" << endl;
1117 // Ugly code that tries to make the result prettier
1118 size_t i = out.find("\\mathchoice");
1120 size_t j = get_matching_brace(out, i + 12);
1121 size_t k = get_matching_brace(out, j + 1);
1122 k = get_matching_brace(out, k + 1);
1123 k = get_matching_brace(out, k + 1);
1124 string mid = out.substr(i + 13, j - i - 13);
1125 if (mid.find("\\over") != npos)
1126 mid = '{' + mid + '}';
1127 out = out.substr(0, i)
1129 + out.substr(k + 1);
1130 //lyxerr << "output: " << out << endl;
1131 i = out.find("\\mathchoice", i);
1134 i = out.find("\\over");
1136 size_t j = get_matching_brace_back(out, i - 1);
1137 if (j == npos || j == 0)
1139 size_t k = get_matching_brace(out, i + 5);
1140 if (k == npos || k + 1 == out.size())
1142 out = out.substr(0, j - 1)
1144 + out.substr(j, i - j)
1145 + out.substr(i + 5, k - i - 4)
1146 + out.substr(k + 2);
1147 //lyxerr << "output: " << out << endl;
1148 i = out.find("\\over", i + 4);
1151 mathed_parse_cell(res, from_utf8(out));
1156 MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1158 string header = "readlib(latex):\n";
1160 // remove the \\it for variable names
1161 //"#`latex/csname_font` := `\\it `:"
1163 "`latex/csname_font` := ``:\n";
1165 // export matrices in (...) instead of [...]
1167 "`latex/latex/matrix` := "
1168 "subs(`[`=`(`, `]`=`)`,"
1169 "eval(`latex/latex/matrix`)):\n";
1171 // replace \\cdots with proper '*'
1173 "`latex/latex/*` := "
1174 "subs(`\\,`=`\\cdot `,"
1175 "eval(`latex/latex/*`)):\n";
1177 // remove spurious \\noalign{\\medskip} in matrix output
1179 "`latex/latex/matrix`:= "
1180 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1181 "eval(`latex/latex/matrix`)):\n";
1183 //"#`latex/latex/symbol` "
1184 // " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1186 string trailer = "quit;";
1187 odocstringstream os;
1190 string expr = to_utf8(os.str());
1191 lyxerr << "ar: '" << ar << "'\n"
1192 << "ms: '" << expr << "'" << endl;
1194 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1195 // try to fix missing '*' the hard way by using mint
1197 // ... > echo "1A;" | mint -i 1 -S -s -q
1200 // Probably missing an operator such as * p
1202 lyxerr << "checking expr: '" << expr << "'" << endl;
1203 string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1205 break; // expression syntax is ok
1206 istringstream is(out);
1209 if (!prefixIs(line, "on line"))
1210 break; // error message not identified
1212 size_t pos = line.find('^');
1213 if (pos == string::npos || pos < 15)
1214 break; // caret position not found
1215 pos -= 15; // skip the "on line ..." part
1216 if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1217 break; // two '*' in a row are definitely bad
1218 expr.insert(pos, 1, '*');
1221 // FIXME UNICODE Is utf8 encoding correct?
1222 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1223 string out = captureOutput("maple -q", header + full + trailer);
1229 mathed_parse_cell(res, from_utf8(out));
1234 MathData pipeThroughOctave(docstring const &, MathData const & ar)
1236 odocstringstream os;
1237 OctaveStream vs(os);
1239 string expr = to_utf8(os.str());
1242 Buffer * buf = const_cast<Buffer *>(ar.buffer());
1243 lyxerr << "pipe: ar: '" << ar << "'\n"
1244 << "pipe: expr: '" << expr << "'" << endl;
1246 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1248 // try to fix missing '*' the hard way
1250 // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1253 lyxerr << "checking expr: '" << expr << "'" << endl;
1254 out = captureOutput("octave -q 2>&1", expr);
1255 lyxerr << "output: '" << out << "'" << endl;
1257 // leave loop if expression syntax is probably ok
1258 if (out.find("parse error:") == string::npos)
1261 // search line with single caret
1262 istringstream is(out);
1266 lyxerr << "skipping line: '" << line << "'" << endl;
1267 if (line.find(">>> ") != string::npos)
1271 // found line with error, next line is the one with caret
1273 size_t pos = line.find('^');
1274 lyxerr << "caret line: '" << line << "'" << endl;
1275 lyxerr << "found caret at pos: '" << pos << "'" << endl;
1276 if (pos == string::npos || pos < 4)
1277 break; // caret position not found
1278 pos -= 4; // skip the ">>> " part
1279 if (expr[pos] == '*')
1280 break; // two '*' in a row are definitely bad
1281 expr.insert(pos, 1, '*');
1284 // remove 'ans = ' taking into account that there may be an
1285 // ansi control sequence before, such as '\033[?1034hans = '
1286 size_t i = out.find("ans = ");
1287 if (i == string::npos)
1289 out = out.substr(i + 6);
1291 // parse output as matrix or single number
1292 MathAtom at(new InsetMathArray(buf, from_ascii("array"), from_utf8(out)));
1293 InsetMathArray const * mat = at->asArrayInset();
1295 if (mat->ncols() == 1 && mat->nrows() == 1)
1296 res.append(mat->cell(0));
1298 res.push_back(MathAtom(
1299 new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
1300 res.back().nucleus()->cell(0).push_back(at);
1306 string fromMathematicaName(string const & name)
1308 if (name == "Sin") return "sin";
1309 if (name == "Sinh") return "sinh";
1310 if (name == "ArcSin") return "arcsin";
1311 if (name == "Cos") return "cos";
1312 if (name == "Cosh") return "cosh";
1313 if (name == "ArcCos") return "arccos";
1314 if (name == "Tan") return "tan";
1315 if (name == "Tanh") return "tanh";
1316 if (name == "ArcTan") return "arctan";
1317 if (name == "Cot") return "cot";
1318 if (name == "Coth") return "coth";
1319 if (name == "Csc") return "csc";
1320 if (name == "Sec") return "sec";
1321 if (name == "Exp") return "exp";
1322 if (name == "Log") return "log";
1323 if (name == "Arg" ) return "arg";
1324 if (name == "Det" ) return "det";
1325 if (name == "GCD" ) return "gcd";
1326 if (name == "Max" ) return "max";
1327 if (name == "Min" ) return "min";
1328 if (name == "Erf" ) return "erf";
1329 if (name == "Erfc" ) return "erfc";
1334 void prettifyMathematicaOutput(string & out, string const & macroName,
1335 bool roman, bool translate)
1337 string const macro = "\\" + macroName + "{";
1338 size_t const len = macro.length();
1339 size_t i = out.find(macro);
1342 size_t const j = get_matching_brace(out, i + len);
1343 string const name = out.substr(i + len, j - i - len);
1344 out = out.substr(0, i)
1345 + (roman ? "\\mathrm{" : "")
1346 + (translate ? fromMathematicaName(name) : name)
1347 + out.substr(roman ? j : j + 1);
1348 //lyxerr << "output: " << out << endl;
1349 i = out.find(macro, i);
1354 MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1356 odocstringstream os;
1357 MathematicaStream ms(os);
1359 // FIXME UNICODE Is utf8 encoding correct?
1360 string const expr = to_utf8(os.str());
1363 lyxerr << "expr: '" << expr << "'" << endl;
1365 string const full = "TeXForm[" + expr + "]";
1366 out = captureOutput("math", full);
1367 lyxerr << "output: '" << out << "'" << endl;
1369 size_t pos1 = out.find("Out[1]//TeXForm= ");
1370 size_t pos2 = out.find("In[2]:=");
1372 if (pos1 == string::npos || pos2 == string::npos)
1375 // get everything from pos1+17 to pos2
1376 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1377 out = subst(subst(out, '\r', ' '), '\n', ' ');
1379 // tries to make the result prettier
1380 prettifyMathematicaOutput(out, "Mfunction", true, true);
1381 prettifyMathematicaOutput(out, "Muserfunction", true, false);
1382 prettifyMathematicaOutput(out, "Mvariable", false, false);
1385 mathed_parse_cell(res, from_utf8(out));
1393 void write(MathData const & dat, WriteStream & wi)
1395 wi.firstitem() = true;
1397 for (MathData::const_iterator it = dat.begin(); it != dat.end(); ++it) {
1398 InsetMathChar const * const c = (*it)->asCharInset();
1407 wi.firstitem() = false;
1412 wi.firstitem() = false;
1417 void writeString(docstring const & s, WriteStream & os)
1419 if (!os.latex() || os.lockedMode()) {
1420 os << (os.asciiOnly() ? escape(s) : s);
1424 docstring::const_iterator cit = s.begin();
1425 docstring::const_iterator end = s.end();
1427 // We may already be inside an \ensuremath command.
1428 bool in_forced_mode = os.pendingBrace();
1430 // We will take care of matching braces.
1431 os.pendingBrace(false);
1433 while (cit != end) {
1434 bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
1435 char_type const c = *cit;
1436 docstring command(1, c);
1438 bool termination = false;
1440 Encodings::latexMathChar(c, mathmode, os.encoding(), command, termination)) {
1441 if (os.textMode()) {
1442 if (in_forced_mode) {
1443 // we were inside \lyxmathsym
1446 in_forced_mode = false;
1448 if (!isASCII(c) && os.textMode()) {
1449 os << "\\ensuremath{";
1451 in_forced_mode = true;
1453 } else if (isASCII(c) && in_forced_mode) {
1454 // we were inside \ensuremath
1457 in_forced_mode = false;
1459 } else if (!os.textMode()) {
1460 if (in_forced_mode) {
1461 // we were inside \ensuremath
1463 in_forced_mode = false;
1465 os << "\\lyxmathsym{";
1466 in_forced_mode = true;
1471 // We may need a space if the command contains a macro
1472 // and the last char is ASCII.
1474 os.pendingSpace(true);
1475 } catch (EncodingException const & e) {
1476 switch (os.output()) {
1477 case WriteStream::wsDryrun: {
1478 os << "<" << _("LyX Warning: ")
1479 << _("uncodable character") << " '";
1480 os << docstring(1, e.failed_char);
1484 case WriteStream::wsPreview: {
1485 // indicate the encoding error by a boxed '?'
1486 os << "{\\fboxsep=1pt\\fbox{?}}";
1487 LYXERR0("Uncodable character" << " '"
1488 << docstring(1, e.failed_char)
1492 case WriteStream::wsDefault:
1501 if (in_forced_mode && os.textMode()) {
1502 // We have to care for closing \lyxmathsym
1506 os.pendingBrace(in_forced_mode);
1511 void normalize(MathData const & ar, NormalStream & os)
1513 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1514 (*it)->normalize(os);
1518 void octave(MathData const & dat, OctaveStream & os)
1521 extractStructure(ar, OCTAVE);
1522 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1527 void maple(MathData const & dat, MapleStream & os)
1530 extractStructure(ar, MAPLE);
1531 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1536 void maxima(MathData const & dat, MaximaStream & os)
1539 extractStructure(ar, MAXIMA);
1540 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1545 void mathematica(MathData const & dat, MathematicaStream & os)
1548 extractStructure(ar, MATHEMATICA);
1549 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1550 (*it)->mathematica(os);
1554 void mathmlize(MathData const & dat, MathStream & os)
1557 extractStructure(ar, MATHML);
1560 else if (ar.size() == 1)
1564 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1565 (*it)->mathmlize(os);
1571 void htmlize(MathData const & dat, HtmlStream & os)
1574 extractStructure(ar, HTML);
1577 if (ar.size() == 1) {
1581 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1586 // convert this inset somehow to a number
1587 bool extractNumber(MathData const & ar, int & i)
1589 idocstringstream is(charSequence(ar.begin(), ar.end()));
1591 // Do not convert is implicitly to bool, since that is forbidden in C++11.
1596 bool extractNumber(MathData const & ar, double & d)
1598 idocstringstream is(charSequence(ar.begin(), ar.end()));
1600 // Do not convert is implicitly to bool, since that is forbidden in C++11.
1605 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1606 MathData const & ar)
1608 if (lang == "octave")
1609 return pipeThroughOctave(extra, ar);
1611 if (lang == "maxima")
1612 return pipeThroughMaxima(extra, ar);
1614 if (lang == "maple")
1615 return pipeThroughMaple(extra, ar);
1617 if (lang == "mathematica")
1618 return pipeThroughMathematica(extra, ar);
1620 // create normalized expression
1621 odocstringstream os;
1622 NormalStream ns(os);
1623 os << '[' << extra << ' ';
1626 // FIXME UNICODE Is utf8 encoding correct?
1627 string data = to_utf8(os.str());
1629 // search external script
1630 FileName const file = libFileSearch("mathed", "extern_" + lang);
1632 lyxerr << "converter to '" << lang << "' not found" << endl;
1636 // run external sript
1637 string out = captureOutput(file.absFileName(), data);
1639 mathed_parse_cell(res, from_utf8(out));