]> git.lyx.org Git - lyx.git/blob - src/mathed/MathExtern.cpp
Fix compilation with libc++
[lyx.git] / src / mathed / MathExtern.cpp
1 /**
2  * \file MathExtern.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 // This file contains most of the magic that extracts "context
12 // information" from the unstructered layout-oriented stuff in
13 // MathData.
14
15 #include <config.h>
16
17 #include "MathExtern.h"
18
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"
34 #include "MathData.h"
35 #include "MathParser.h"
36 #include "MathStream.h"
37
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"
43
44 #include <algorithm>
45 #include <sstream>
46 #include <fstream>
47 #include <memory>
48
49 using namespace std;
50 using namespace lyx::support;
51
52 namespace lyx {
53
54 namespace {
55
56 enum ExternalMath {
57         HTML,
58         MAPLE,
59         MAXIMA,
60         MATHEMATICA,
61         MATHML,
62         OCTAVE
63 };
64
65
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
73 };
74
75 static size_t const npos = lyx::docstring::npos;
76
77 // define a function for tests
78 typedef bool TestItemFunc(MathAtom const &);
79
80 // define a function for replacing subexpressions
81 typedef MathAtom ReplaceArgumentFunc(const MathData & ar);
82
83
84
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)
89 {
90         // nothing to get here
91         if (pos == last)
92                 return false;
93
94         // is this a scriptinset?
95         if (!(*pos)->asScriptInset())
96                 return false;
97
98         // do we want superscripts only?
99         if (superscript && !(*pos)->asScriptInset()->hasUp())
100                 return false;
101
102         // it is a scriptinset, use it.
103         ar.push_back(*pos);
104         ++pos;
105         return true;
106 }
107
108
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)
114 {
115         // nothing to get here
116         if (pos == last)
117                 return pos;
118
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
123                 // the delimiters
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();
128                         while (cur != end)
129                                 ar.push_back(*cur++);
130                 } else
131                         ar.push_back(*pos);
132                 ++pos;
133                 if (pos == last)
134                         return pos;
135                 // if there's one, get following superscript only if this
136                 // isn't a function argument
137                 if (!function)
138                         extractScript(ar, pos, last, true);
139                 return pos;
140         }
141
142         // always take the first thing, no matter what it is
143         ar.push_back(*pos);
144
145         // go ahead if possible
146         ++pos;
147         if (pos == last)
148                 return pos;
149
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);
153         if (pos == last)
154                 return pos;
155
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
160         //      if (it == pos) {
161         //              ar.push_back(*it);
162         //              continue;
163         //      }
164         //}
165         return pos;
166 }
167
168
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)
173 {
174         docstring s;
175         for (; it != end && (*it)->asCharInset(); ++it)
176                 s += (*it)->getChar();
177         return s;
178 }
179
180
181 void extractStrings(MathData & ar)
182 {
183         //lyxerr << "\nStrings from: " << ar << endl;
184         for (size_t i = 0; i < ar.size(); ++i) {
185                 if (!ar[i]->asCharInset())
186                         continue;
187                 docstring s = charSequence(ar.begin() + i, ar.end());
188                 ar[i] = MathAtom(new InsetMathString(s));
189                 ar.erase(i + 1, i + s.size());
190         }
191         //lyxerr << "\nStrings to: " << ar << endl;
192 }
193
194
195 void extractMatrices(MathData & ar)
196 {
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();
201                 if (!inset)
202                         continue;
203                 MathData const & arr = inset->cell(0);
204                 if (arr.size() != 1)
205                         continue;
206                 if (!arr.front()->asGridInset())
207                         continue;
208                 ar[i] = MathAtom(new InsetMathMatrix(*(arr.front()->asGridInset()), 
209                                  inset->left_, inset->right_));
210         }
211
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();
215                 if (inset) {
216                         string left = inset->name_left();
217                         if (left == "Vert")
218                                 left = "[";
219                         string right = inset->name_right();
220                         if (right == "Vert")
221                                 right = "]";
222                         ar[i] = MathAtom(new InsetMathMatrix(*inset, from_ascii(left), from_ascii(right)));
223                 }
224         }
225         //lyxerr << "\nMatrices to: " << ar << endl;
226 }
227
228
229 // convert this inset somehow to a string
230 bool extractString(MathAtom const & at, docstring & str)
231 {
232         if (at->getChar()) {
233                 str = docstring(1, at->getChar());
234                 return true;
235         }
236         if (at->asStringInset()) {
237                 str = at->asStringInset()->str();
238                 return true;
239         }
240         return false;
241 }
242
243
244 // is this a known function?
245 bool isKnownFunction(docstring const & str)
246 {
247         for (int i = 0; function_names[i]; ++i) {
248                 if (str == function_names[i])
249                         return true;
250         }
251         return false;
252 }
253
254
255 // extract a function name from this inset
256 bool extractFunctionName(MathAtom const & at, docstring & str)
257 {
258         if (at->asSymbolInset()) {
259                 str = at->asSymbolInset()->name();
260                 return isKnownFunction(str);
261         }
262         if (at->asUnknownInset()) {
263                 // assume it is well known...
264                 str = at->name();
265                 return true;
266         }
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();
272         }
273         return false;
274 }
275
276
277 bool testString(MathAtom const & at, docstring const & str)
278 {
279         docstring s;
280         return extractString(at, s) && str == s;
281 }
282
283
284 bool testString(MathAtom const & at, char const * const str)
285 {
286         return testString(at, from_ascii(str));
287 }
288
289 // search end of nested sequence
290 MathData::iterator endNestSearch(
291         MathData::iterator it,
292         MathData::iterator last,
293         TestItemFunc testOpen,
294         TestItemFunc testClose
295 )
296 {
297         for (int level = 0; it != last; ++it) {
298                 if (testOpen(*it))
299                         ++level;
300                 if (testClose(*it))
301                         --level;
302                 if (level == 0)
303                         break;
304         }
305         return it;
306 }
307
308
309 // replace nested sequences by a real Insets
310 void replaceNested(
311         MathData & ar,
312         TestItemFunc testOpen,
313         TestItemFunc testClose,
314         ReplaceArgumentFunc replaceArg)
315 {
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]))
322                         continue;
323
324                 // search end of sequence
325                 MathData::iterator it = ar.begin() + i;
326                 MathData::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
327                 if (jt == ar.end())
328                         continue;
329
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);
333         }
334 }
335
336
337
338 //
339 // split scripts into seperate super- and subscript insets. sub goes in
340 // front of super...
341 //
342
343 void splitScripts(MathData & ar)
344 {
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();
349
350                 // is this a script inset and do we also have a superscript?
351                 if (!script || !script->hasUp())
352                         continue;
353
354                 // we must have a nucleus if we only have a superscript
355                 if (!script->hasDown() && script->nuc().empty())
356                         continue;
357
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"))
363                                 continue;
364                 }
365
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);
371
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();
377                         ar.erase(i);
378                         while (it != et)
379                                 ar.insert(i++, *it++);
380                 } else
381                         ++i;
382
383                 // insert new inset behind
384                 ar.insert(i, MathAtom(q.release()));
385         }
386         //lyxerr << "\nScripts to: " << ar << endl;
387 }
388
389
390 //
391 // extract exp(...)
392 //
393
394 void extractExps(MathData & ar)
395 {
396         Buffer * buf = ar.buffer();
397         //lyxerr << "\nExps from: " << ar << endl;
398         for (size_t i = 0; i + 1 < ar.size(); ++i) {
399                 // is this 'e'?
400                 if (ar[i]->getChar() != 'e')
401                         continue;
402
403                 // we need an exponent but no subscript
404                 InsetMathScript const * sup = ar[i + 1]->asScriptInset();
405                 if (!sup || sup->hasDown())
406                         continue;
407
408                 // create a proper exp-inset as replacement
409                 ar[i] = MathAtom(new InsetMathExFunc(buf, from_ascii("exp"), sup->cell(1)));
410                 ar.erase(i + 1);
411         }
412         //lyxerr << "\nExps to: " << ar << endl;
413 }
414
415
416 //
417 // extract det(...)  from |matrix|
418 //
419 void extractDets(MathData & ar)
420 {
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();
425                 if (!del)
426                         continue;
427                 if (!del->isAbs())
428                         continue;
429                 *it = MathAtom(new InsetMathExFunc(buf, from_ascii("det"), del->cell(0)));
430         }
431         //lyxerr << "\ndet to: " << ar << endl;
432 }
433
434
435 //
436 // search numbers
437 //
438
439 bool isDigitOrSimilar(char_type c)
440 {
441         return ('0' <= c && c <= '9') || c == '.';
442 }
443
444
445 // returns sequence of digits
446 docstring digitSequence
447         (MathData::const_iterator it, MathData::const_iterator end)
448 {
449         docstring s;
450         for (; it != end && (*it)->asCharInset(); ++it) {
451                 if (!isDigitOrSimilar((*it)->getChar()))
452                         break;
453                 s += (*it)->getChar();
454         }
455         return s;
456 }
457
458
459 void extractNumbers(MathData & ar)
460 {
461         //lyxerr << "\nNumbers from: " << ar << endl;
462         for (size_t i = 0; i < ar.size(); ++i) {
463                 if (!ar[i]->asCharInset())
464                         continue;
465                 if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
466                         continue;
467
468                 docstring s = digitSequence(ar.begin() + i, ar.end());
469
470                 ar[i] = MathAtom(new InsetMathNumber(s));
471                 ar.erase(i + 1, i + s.size());
472         }
473         //lyxerr << "\nNumbers to: " << ar << endl;
474 }
475
476
477
478 //
479 // search delimiters
480 //
481
482 bool testOpenParen(MathAtom const & at)
483 {
484         return testString(at, "(");
485 }
486
487
488 bool testCloseParen(MathAtom const & at)
489 {
490         return testString(at, ")");
491 }
492
493
494 MathAtom replaceParenDelims(const MathData & ar)
495 {
496         return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
497                 from_ascii("("), from_ascii(")"), ar));
498 }
499
500
501 bool testOpenBracket(MathAtom const & at)
502 {
503         return testString(at, "[");
504 }
505
506
507 bool testCloseBracket(MathAtom const & at)
508 {
509         return testString(at, "]");
510 }
511
512
513 MathAtom replaceBracketDelims(const MathData & ar)
514 {
515         return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
516                 from_ascii("["), from_ascii("]"), ar));
517 }
518
519
520 // replace '('...')' and '['...']' sequences by a real InsetMathDelim
521 void extractDelims(MathData & ar)
522 {
523         //lyxerr << "\nDelims from: " << ar << endl;
524         replaceNested(ar, testOpenParen, testCloseParen, replaceParenDelims);
525         replaceNested(ar, testOpenBracket, testCloseBracket, replaceBracketDelims);
526         //lyxerr << "\nDelims to: " << ar << endl;
527 }
528
529
530
531 //
532 // search well-known functions
533 //
534
535
536 // replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real InsetMathExFunc
537 // assume 'extractDelims' ran before
538 void extractFunctions(MathData & ar, ExternalMath kind)
539 {
540         // FIXME From what I can see, this is quite broken right now, for reasons
541         // I will note below. (RGH)
542
543         // we need at least two items...
544         if (ar.size() < 2)
545                 return;
546
547         Buffer * buf = ar.buffer();
548
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;
553
554                 docstring name;
555                 // is it a function?
556                 // it certainly is if it is well known...
557
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
563                 // rather "n(x)".
564                 //
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.
569                 //
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
573                 // guessing.
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))
578                                 continue;
579                         // it is not if it has no argument
580                         if (jt == ar.end())
581                                 continue;
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)
586                                 continue;
587                         // fall through into main branch
588                 }
589
590                 // do we have an exponent like in
591                 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
592                 MathData exp;
593                 extractScript(exp, jt, ar.end(), true);
594
595                 // create a proper inset as replacement
596                 auto_ptr<InsetMathExFunc> p(new InsetMathExFunc(buf, name));
597
598                 // jt points to the "argument". Get hold of this.
599                 MathData::iterator st = 
600                                 extractArgument(p->cell(0), jt, ar.end(), kind, true);
601
602                 // replace the function name by a real function inset
603                 *it = MathAtom(p.release());
604
605                 // remove the source of the argument from the array
606                 ar.erase(it + 1, st);
607
608                 // re-insert exponent
609                 ar.insert(i + 1, exp);
610                 //lyxerr << "\nFunctions to: " << ar << endl;
611         }
612 }
613
614
615 //
616 // search integrals
617 //
618
619 bool testSymbol(MathAtom const & at, docstring const & name)
620 {
621         return at->asSymbolInset() && at->asSymbolInset()->name() == name;
622 }
623
624
625 bool testSymbol(MathAtom const & at, char const * const name)
626 {
627         return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
628 }
629
630
631 bool testIntSymbol(MathAtom const & at)
632 {
633         return testSymbol(at, from_ascii("int"));
634 }
635
636
637 bool testIntegral(MathAtom const & at)
638 {
639         return
640          testIntSymbol(at) ||
641                 ( at->asScriptInset()
642                   && !at->asScriptInset()->nuc().empty()
643                         && testIntSymbol(at->asScriptInset()->nuc().back()) );
644 }
645
646
647
648 bool testIntDiff(MathAtom const & at)
649 {
650         return testString(at, "d");
651 }
652
653
654 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
655 // assume 'extractDelims' ran before
656 void extractIntegrals(MathData & ar, ExternalMath kind)
657 {
658         // we need at least three items...
659         if (ar.size() < 3)
660                 return;
661
662         Buffer * buf = ar.buffer();
663
664         //lyxerr << "\nIntegrals from: " << ar << endl;
665         for (size_t i = 0; i + 1 < ar.size(); ++i) {
666                 MathData::iterator it = ar.begin() + i;
667
668                 // search 'd'
669                 MathData::iterator jt =
670                         endNestSearch(it, ar.end(), testIntegral, testIntDiff);
671
672                 // something sensible found?
673                 if (jt == ar.end())
674                         continue;
675
676                 // is this a integral name?
677                 if (!testIntegral(*it))
678                         continue;
679
680                 // core ist part from behind the scripts to the 'd'
681                 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("int")));
682
683                 // handle scripts if available
684                 if (!testIntSymbol(*it)) {
685                         p->cell(2) = (*it)->asScriptInset()->down();
686                         p->cell(3) = (*it)->asScriptInset()->up();
687                 }
688                 p->cell(0) = MathData(buf, it + 1, jt);
689
690                 // use the "thing" behind the 'd' as differential
691                 MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end(), kind);
692
693                 // remove used parts
694                 ar.erase(it + 1, tt);
695                 *it = MathAtom(p.release());
696         }
697         //lyxerr << "\nIntegrals to: " << ar << endl;
698 }
699
700
701 bool testTermDelimiter(MathAtom const & at)
702 {
703         return testString(at, "+") || testString(at, "-");
704 }
705
706
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)
711 {
712         while (pos != last && !testTermDelimiter(*pos)) {
713                 ar.push_back(*pos);
714                 ++pos;
715         }
716         return pos;
717 }
718
719
720 //
721 // search sums
722 //
723
724
725 bool testEqualSign(MathAtom const & at)
726 {
727         return testString(at, "=");
728 }
729
730
731 bool testSumSymbol(MathAtom const & p)
732 {
733         return testSymbol(p, from_ascii("sum"));
734 }
735
736
737 bool testSum(MathAtom const & at)
738 {
739         return
740          testSumSymbol(at) ||
741                 ( at->asScriptInset()
742                   && !at->asScriptInset()->nuc().empty()
743                         && testSumSymbol(at->asScriptInset()->nuc().back()) );
744 }
745
746
747 // replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
748 // assume 'extractDelims' ran before
749 void extractSums(MathData & ar)
750 {
751         // we need at least two items...
752         if (ar.size() < 2)
753                 return;
754
755         Buffer * buf = ar.buffer();
756
757         //lyxerr << "\nSums from: " << ar << endl;
758         for (size_t i = 0; i + 1 < ar.size(); ++i) {
759                 MathData::iterator it = ar.begin() + i;
760
761                 // is this a sum name?
762                 if (!testSum(ar[i]))
763                         continue;
764
765                 // create a proper inset as replacement
766                 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("sum")));
767
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());
780                         } else {
781                                 // use everything as summation index, don't use scripts.
782                                 p->cell(1) = ar;
783                         }
784                 }
785
786                 // collect upper bound
787                 if (sub && sub->hasUp())
788                         p->cell(3) = sub->up();
789
790                 // use something  behind the script as core
791                 MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
792
793                 // cleanup
794                 ar.erase(it + 1, tt);
795                 *it = MathAtom(p.release());
796         }
797         //lyxerr << "\nSums to: " << ar << endl;
798 }
799
800
801 //
802 // search differential stuff
803 //
804
805 // tests for 'd' or '\partial'
806 bool testDiffItem(MathAtom const & at)
807 {
808         if (testString(at, "d") || testSymbol(at, "partial"))
809                 return true;
810
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");
816         }
817         return false;
818 }
819
820
821 bool testDiffArray(MathData const & ar)
822 {
823         return !ar.empty() && testDiffItem(ar.front());
824 }
825
826
827 bool testDiffFrac(MathAtom const & at)
828 {
829         return
830                 at->asFracInset()
831                         && testDiffArray(at->asFracInset()->cell(0))
832                         && testDiffArray(at->asFracInset()->cell(1));
833 }
834
835
836 void extractDiff(MathData & ar)
837 {
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;
842
843                 // is this a "differential fraction"?
844                 if (!testDiffFrac(*it))
845                         continue;
846
847                 InsetMathFrac const * f = (*it)->asFracInset();
848                 if (!f) {
849                         lyxerr << "should not happen" << endl;
850                         continue;
851                 }
852
853                 // create a proper diff inset
854                 auto_ptr<InsetMathDiff> diff(new InsetMathDiff(buf));
855
856                 // collect function, let jt point behind last used item
857                 MathData::iterator jt = it + 1;
858                 //int n = 1;
859                 MathData numer(f->cell(0));
860                 splitScripts(numer);
861                 if (numer.size() > 1 && numer[1]->asScriptInset()) {
862                         // this is something like  d^n f(x) / d... or  d^n / d...
863                         // FIXME
864                         //n = 1;
865                         if (numer.size() > 2)
866                                 diff->cell(0) = MathData(buf, numer.begin() + 2, numer.end());
867                         else
868                                 jt = extractTerm(diff->cell(0), jt, ar.end());
869                 } else {
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());
873                         else
874                                 jt = extractTerm(diff->cell(0), jt, ar.end());
875                 }
876
877                 // collect denominator parts
878                 MathData denom(f->cell(1));
879                 splitScripts(denom);
880                 for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
881                         // find the next 'd'
882                         MathData::iterator et
883                                 = find_if(dt + 1, denom.end(), &testDiffItem);
884
885                         // point before this
886                         MathData::iterator st = et - 1;
887                         InsetMathScript const * script = (*st)->asScriptInset();
888                         if (script && script->hasUp()) {
889                                 // things like   d.../dx^n
890                                 int mult = 1;
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));
895                                 }
896                         } else {
897                                 // just  d.../dx
898                                 diff->addDer(MathData(buf, dt + 1, et));
899                         }
900                         dt = et;
901                 }
902
903                 // cleanup
904                 ar.erase(it + 1, jt);
905                 *it = MathAtom(diff.release());
906         }
907         //lyxerr << "\nDiffs to: " << ar << endl;
908 }
909
910
911 //
912 // search limits
913 //
914
915
916 bool testRightArrow(MathAtom const & at)
917 {
918         return testSymbol(at, "to") || testSymbol(at, "rightarrow");
919 }
920
921
922
923 // replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
924 // assume 'extractDelims' ran before
925 void extractLims(MathData & ar)
926 {
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;
931
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)
935                         continue;
936
937                 // is this a limit function?
938                 if (!testSymbol(sub->nuc().front(), "lim"))
939                         continue;
940
941                 // subscript must contain a -> symbol
942                 MathData const & s = sub->down();
943                 MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
944                 if (st == s.end())
945                         continue;
946
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());
950
951                 // use something behind the script as core
952                 MathData f;
953                 MathData::iterator tt = extractTerm(f, it + 1, ar.end());
954
955                 // cleanup
956                 ar.erase(it + 1, tt);
957
958                 // create a proper inset as replacement
959                 *it = MathAtom(new InsetMathLim(buf, f, x, x0));
960         }
961         //lyxerr << "\nLimits to: " << ar << endl;
962 }
963
964
965 //
966 // combine searches
967 //
968
969 void extractStructure(MathData & ar, ExternalMath kind)
970 {
971         //lyxerr << "\nStructure from: " << ar << endl;
972         if (kind != MATHML && kind != HTML)
973                 splitScripts(ar);
974         extractDelims(ar);
975         extractIntegrals(ar, kind);
976         if (kind != MATHML && kind != HTML)
977                 extractSums(ar);
978         extractNumbers(ar);
979         extractMatrices(ar);
980         if (kind != MATHML && kind != HTML) {
981                 extractFunctions(ar, kind);
982                 extractDets(ar);
983                 extractDiff(ar);
984                 extractExps(ar);
985                 extractLims(ar);
986                 extractStrings(ar);
987         }
988         //lyxerr << "\nStructure to: " << ar << endl;
989 }
990
991
992 namespace {
993
994         string captureOutput(string const & cmd, string const & data)
995         {
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."
1001                                << endl;
1002                         return string();
1003                 }
1004                 ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
1005                 os << data << endl;
1006                 os.close();
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();
1013                 return ret.second;
1014         }
1015
1016         size_t get_matching_brace(string const & str, size_t i)
1017         {
1018                 int count = 1;
1019                 size_t n = str.size();
1020                 while (i < n) {
1021                         i = str.find_first_of("{}", i+1);
1022                         if (i == npos)
1023                                 return i;
1024                         if (str[i] == '{')
1025                                 ++count;
1026                         else
1027                                 --count;
1028                         if (count == 0)
1029                                 return i;
1030                 }
1031                 return npos;
1032         }
1033
1034         size_t get_matching_brace_back(string const & str, size_t i)
1035         {
1036                 int count = 1;
1037                 while (i > 0) {
1038                         i = str.find_last_of("{}", i-1);
1039                         if (i == npos)
1040                                 return i;
1041                         if (str[i] == '}')
1042                                 ++count;
1043                         else
1044                                 --count;
1045                         if (count == 0)
1046                                 return i;
1047                 }
1048                 return npos;
1049         }
1050
1051         MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1052         {
1053                 odocstringstream os;
1054                 MaximaStream ms(os);
1055                 ms << ar;
1056                 docstring expr = os.str();
1057                 docstring const header = from_ascii("simpsum:true;");
1058
1059                 string out;
1060                 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1061                         // try to fix missing '*' the hard way
1062                         //
1063                         // > echo "2x;" | maxima
1064                         // ...
1065                         // (C1) Incorrect syntax: x is not an infix operator
1066                         // 2x;
1067                         //  ^
1068                         //
1069                         lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1070                         docstring full = header + "tex(" + expr + ");";
1071                         out = captureOutput("maxima", to_utf8(full));
1072
1073                         // leave loop if expression syntax is probably ok
1074                         if (out.find("Incorrect syntax") == npos)
1075                                 break;
1076
1077                         // search line with "Incorrect syntax"
1078                         istringstream is(out);
1079                         string line;
1080                         while (is) {
1081                                 getline(is, line);
1082                                 if (line.find("Incorrect syntax") != npos)
1083                                         break;
1084                         }
1085
1086                         // 2nd next line is the one with caret
1087                         getline(is, line);
1088                         getline(is, line);
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("*"));
1097                 }
1098
1099                 vector<string> tmp = getVectorFromString(out, "$$");
1100                 if (tmp.size() < 2)
1101                         return MathData();
1102
1103                 out = subst(tmp[1], "\\>", string());
1104                 lyxerr << "output: '" << out << "'" << endl;
1105
1106                 // Ugly code that tries to make the result prettier
1107                 size_t i = out.find("\\mathchoice");
1108                 while (i != npos) {
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)
1117                                 + mid
1118                                 + out.substr(k + 1);
1119                         //lyxerr << "output: " << out << endl;
1120                         i = out.find("\\mathchoice", i);
1121                         break;
1122                 }
1123
1124                 i = out.find("\\over");
1125                 while (i != npos) {
1126                         size_t j = get_matching_brace_back(out, i - 1);
1127                         if (j == npos || j == 0)
1128                                 break;
1129                         size_t k = get_matching_brace(out, i + 5);
1130                         if (k == npos || k + 1 == out.size())
1131                                 break;
1132                         out = out.substr(0, j - 1)
1133                                 + "\\frac"
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);
1139                 }
1140                 MathData res;
1141                 mathed_parse_cell(res, from_utf8(out));
1142                 return res;
1143         }
1144
1145
1146         MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1147         {
1148                 string header = "readlib(latex):\n";
1149
1150                 // remove the \\it for variable names
1151                 //"#`latex/csname_font` := `\\it `:"
1152                 header +=
1153                         "`latex/csname_font` := ``:\n";
1154
1155                 // export matrices in (...) instead of [...]
1156                 header +=
1157                         "`latex/latex/matrix` := "
1158                                 "subs(`[`=`(`, `]`=`)`,"
1159                                         "eval(`latex/latex/matrix`)):\n";
1160
1161                 // replace \\cdots with proper '*'
1162                 header +=
1163                         "`latex/latex/*` := "
1164                                 "subs(`\\,`=`\\cdot `,"
1165                                         "eval(`latex/latex/*`)):\n";
1166
1167                 // remove spurious \\noalign{\\medskip} in matrix output
1168                 header +=
1169                         "`latex/latex/matrix`:= "
1170                                 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1171                                         "eval(`latex/latex/matrix`)):\n";
1172
1173                 //"#`latex/latex/symbol` "
1174                 //      " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1175
1176                 string trailer = "quit;";
1177                 odocstringstream os;
1178                 MapleStream ms(os);
1179                 ms << ar;
1180                 string expr = to_utf8(os.str());
1181                 lyxerr << "ar: '" << ar << "'\n"
1182                        << "ms: '" << expr << "'" << endl;
1183
1184                 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1185                         // try to fix missing '*' the hard way by using mint
1186                         //
1187                         // ... > echo "1A;" | mint -i 1 -S -s -q
1188                         // on line     1: 1A;
1189                         //                 ^ syntax error -
1190                         //                   Probably missing an operator such as * p
1191                         //
1192                         lyxerr << "checking expr: '" << expr << "'" << endl;
1193                         string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1194                         if (out.empty())
1195                                 break; // expression syntax is ok
1196                         istringstream is(out);
1197                         string line;
1198                         getline(is, line);
1199                         if (line.find("on line") != 0)
1200                                 break; // error message not identified
1201                         getline(is, line);
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, '*');
1209                 }
1210
1211                 // FIXME UNICODE Is utf8 encoding correct?
1212                 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1213                 string out = captureOutput("maple -q", header + full + trailer);
1214
1215                 // change \_ into _
1216
1217                 //
1218                 MathData res;
1219                 mathed_parse_cell(res, from_utf8(out));
1220                 return res;
1221         }
1222
1223
1224         MathData pipeThroughOctave(docstring const &, MathData const & ar)
1225         {
1226                 odocstringstream os;
1227                 OctaveStream vs(os);
1228                 vs << ar;
1229                 string expr = to_utf8(os.str());
1230                 string out;
1231                 // FIXME const cast
1232                 Buffer * buf = const_cast<Buffer *>(ar.buffer());
1233                 lyxerr << "pipe: ar: '" << ar << "'\n"
1234                        << "pipe: expr: '" << expr << "'" << endl;
1235
1236                 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1237                         //
1238                         // try to fix missing '*' the hard way
1239                         // parse error:
1240                         // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1241                         //                                   ^
1242                         //
1243                         lyxerr << "checking expr: '" << expr << "'" << endl;
1244                         out = captureOutput("octave -q 2>&1", expr);
1245                         lyxerr << "output: '" << out << "'" << endl;
1246
1247                         // leave loop if expression syntax is probably ok
1248                         if (out.find("parse error:") == string::npos)
1249                                 break;
1250
1251                         // search line with single caret
1252                         istringstream is(out);
1253                         string line;
1254                         while (is) {
1255                                 getline(is, line);
1256                                 lyxerr << "skipping line: '" << line << "'" << endl;
1257                                 if (line.find(">>> ") != string::npos)
1258                                         break;
1259                         }
1260
1261                         // found line with error, next line is the one with caret
1262                         getline(is, line);
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, '*');
1272                 }
1273
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)
1278                         return MathData();
1279                 out = out.substr(i + 6);
1280
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();
1284                 MathData res(buf);
1285                 if (mat->ncols() == 1 && mat->nrows() == 1)
1286                         res.append(mat->cell(0));
1287                 else {
1288                         res.push_back(MathAtom(
1289                                 new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
1290                         res.back().nucleus()->cell(0).push_back(at);
1291                 }
1292                 return res;
1293         }
1294
1295
1296         string fromMathematicaName(string const & name)
1297         {
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";
1320                 return name;
1321         }
1322
1323
1324         void prettifyMathematicaOutput(string & out, string const & macroName,
1325                         bool roman, bool translate)
1326         {
1327                 string const macro = "\\" + macroName + "{";
1328                 size_t const len = macro.length();
1329                 size_t i = out.find(macro);
1330
1331                 while (i != npos) {
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);
1340                 }
1341         }
1342
1343
1344         MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1345         {
1346                 odocstringstream os;
1347                 MathematicaStream ms(os);
1348                 ms << ar;
1349                 // FIXME UNICODE Is utf8 encoding correct?
1350                 string const expr = to_utf8(os.str());
1351                 string out;
1352
1353                 lyxerr << "expr: '" << expr << "'" << endl;
1354
1355                 string const full = "TeXForm[" + expr + "]";
1356                 out = captureOutput("math", full);
1357                 lyxerr << "output: '" << out << "'" << endl;
1358
1359                 size_t pos1 = out.find("Out[1]//TeXForm= ");
1360                 size_t pos2 = out.find("In[2]:=");
1361
1362                 if (pos1 == string::npos || pos2 == string::npos)
1363                         return MathData();
1364
1365                 // get everything from pos1+17 to pos2
1366                 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1367                 out = subst(subst(out, '\r', ' '), '\n', ' ');
1368
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);
1373
1374                 MathData res;
1375                 mathed_parse_cell(res, from_utf8(out));
1376                 return res;
1377         }
1378
1379 }
1380
1381 } // anon namespace
1382
1383 void write(MathData const & dat, WriteStream & wi)
1384 {
1385         MathData ar = dat;
1386         extractStrings(ar);
1387         wi.firstitem() = true;
1388         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it) {
1389                 (*it)->write(wi);
1390                 wi.firstitem() = false;
1391         }
1392 }
1393
1394
1395 void normalize(MathData const & ar, NormalStream & os)
1396 {
1397         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1398                 (*it)->normalize(os);
1399 }
1400
1401
1402 void octave(MathData const & dat, OctaveStream & os)
1403 {
1404         MathData ar = dat;
1405         extractStructure(ar, OCTAVE);
1406         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1407                 (*it)->octave(os);
1408 }
1409
1410
1411 void maple(MathData const & dat, MapleStream & os)
1412 {
1413         MathData ar = dat;
1414         extractStructure(ar, MAPLE);
1415         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1416                 (*it)->maple(os);
1417 }
1418
1419
1420 void maxima(MathData const & dat, MaximaStream & os)
1421 {
1422         MathData ar = dat;
1423         extractStructure(ar, MAXIMA);
1424         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1425                 (*it)->maxima(os);
1426 }
1427
1428
1429 void mathematica(MathData const & dat, MathematicaStream & os)
1430 {
1431         MathData ar = dat;
1432         extractStructure(ar, MATHEMATICA);
1433         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1434                 (*it)->mathematica(os);
1435 }
1436
1437
1438 void mathmlize(MathData const & dat, MathStream & os)
1439 {
1440         MathData ar = dat;
1441         extractStructure(ar, MATHML);
1442         if (ar.empty())
1443                 os << "<mrow/>";
1444         else if (ar.size() == 1)
1445                 os << ar.front();
1446         else {
1447                 os << MTag("mrow");
1448                 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1449                         (*it)->mathmlize(os);
1450                 os << ETag("mrow");
1451         }
1452 }
1453
1454
1455 void htmlize(MathData const & dat, HtmlStream & os)
1456 {
1457         MathData ar = dat;
1458         extractStructure(ar, HTML);
1459         if (ar.empty())
1460                 return;
1461         if (ar.size() == 1) {
1462                 os << ar.front();
1463                 return;
1464         }
1465         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1466                 (*it)->htmlize(os);
1467 }
1468
1469
1470 // convert this inset somehow to a number
1471 bool extractNumber(MathData const & ar, int & i)
1472 {
1473         idocstringstream is(charSequence(ar.begin(), ar.end()));
1474         is >> i;
1475         // Do not convert is implicitly to bool, since that is forbidden in C++11.
1476         return !is.fail();
1477 }
1478
1479
1480 bool extractNumber(MathData const & ar, double & d)
1481 {
1482         idocstringstream is(charSequence(ar.begin(), ar.end()));
1483         is >> d;
1484         // Do not convert is implicitly to bool, since that is forbidden in C++11.
1485         return !is.fail();
1486 }
1487
1488
1489 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1490         MathData const & ar)
1491 {
1492         if (lang == "octave")
1493                 return pipeThroughOctave(extra, ar);
1494
1495         if (lang == "maxima")
1496                 return pipeThroughMaxima(extra, ar);
1497
1498         if (lang == "maple")
1499                 return pipeThroughMaple(extra, ar);
1500
1501         if (lang == "mathematica")
1502                 return pipeThroughMathematica(extra, ar);
1503
1504         // create normalized expression
1505         odocstringstream os;
1506         NormalStream ns(os);
1507         os << '[' << extra << ' ';
1508         ns << ar;
1509         os << ']';
1510         // FIXME UNICODE Is utf8 encoding correct?
1511         string data = to_utf8(os.str());
1512
1513         // search external script
1514         FileName const file = libFileSearch("mathed", "extern_" + lang);
1515         if (file.empty()) {
1516                 lyxerr << "converter to '" << lang << "' not found" << endl;
1517                 return MathData();
1518         }
1519
1520         // run external sript
1521         string out = captureOutput(file.absFileName(), data);
1522         MathData res;
1523         mathed_parse_cell(res, from_utf8(out));
1524         return res;
1525 }
1526
1527
1528 } // namespace lyx