]> git.lyx.org Git - lyx.git/blob - src/mathed/MathExtern.cpp
The beginnings of pure HTML output of math, for our internal XHTML
[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().size() == 0)
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         // we need at least two items...
541         if (ar.size() < 2)
542                 return;
543
544         Buffer * buf = ar.buffer();
545
546         //lyxerr << "\nFunctions from: " << ar << endl;
547         for (size_t i = 0; i + 1 < ar.size(); ++i) {
548                 MathData::iterator it = ar.begin() + i;
549                 MathData::iterator jt = it + 1;
550
551                 docstring name;
552                 // is it a function?
553                 // it certainly is if it is well known...
554                 if (!extractFunctionName(*it, name)) {
555                         // is this a user defined function?
556                         // it it probably not, if it doesn't have a name.
557                         if (!extractString(*it, name))
558                                 continue;
559                         // it is not if it has no argument
560                         if (jt == ar.end())
561                                 continue;
562                         // guess so, if this is followed by
563                         // a DelimInset with a single item in the cell
564                         InsetMathDelim const * del = (*jt)->asDelimInset();
565                         if (!del || del->cell(0).size() != 1)
566                                 continue;
567                         // fall trough into main branch
568                 }
569
570                 // do we have an exponent like in
571                 // 'sin' '^2' 'x' -> 'sin(x)' '^2'
572                 MathData exp;
573                 extractScript(exp, jt, ar.end(), true);
574
575                 // create a proper inset as replacement
576                 auto_ptr<InsetMathExFunc> p(new InsetMathExFunc(buf, name));
577
578                 // jt points to the "argument". Get hold of this.
579                 MathData::iterator st = 
580                                 extractArgument(p->cell(0), jt, ar.end(), kind, true);
581
582                 // replace the function name by a real function inset
583                 *it = MathAtom(p.release());
584
585                 // remove the source of the argument from the array
586                 ar.erase(it + 1, st);
587
588                 // re-insert exponent
589                 ar.insert(i + 1, exp);
590                 //lyxerr << "\nFunctions to: " << ar << endl;
591         }
592 }
593
594
595 //
596 // search integrals
597 //
598
599 bool testSymbol(MathAtom const & at, docstring const & name)
600 {
601         return at->asSymbolInset() && at->asSymbolInset()->name() == name;
602 }
603
604
605 bool testSymbol(MathAtom const & at, char const * const name)
606 {
607         return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
608 }
609
610
611 bool testIntSymbol(MathAtom const & at)
612 {
613         return testSymbol(at, from_ascii("int"));
614 }
615
616
617 bool testIntegral(MathAtom const & at)
618 {
619         return
620          testIntSymbol(at) ||
621                 ( at->asScriptInset()
622                   && at->asScriptInset()->nuc().size()
623                         && testIntSymbol(at->asScriptInset()->nuc().back()) );
624 }
625
626
627
628 bool testIntDiff(MathAtom const & at)
629 {
630         return testString(at, "d");
631 }
632
633
634 // replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
635 // assume 'extractDelims' ran before
636 void extractIntegrals(MathData & ar, ExternalMath kind)
637 {
638         // we need at least three items...
639         if (ar.size() < 3)
640                 return;
641
642         Buffer * buf = ar.buffer();
643
644         //lyxerr << "\nIntegrals from: " << ar << endl;
645         for (size_t i = 0; i + 1 < ar.size(); ++i) {
646                 MathData::iterator it = ar.begin() + i;
647
648                 // search 'd'
649                 MathData::iterator jt =
650                         endNestSearch(it, ar.end(), testIntegral, testIntDiff);
651
652                 // something sensible found?
653                 if (jt == ar.end())
654                         continue;
655
656                 // is this a integral name?
657                 if (!testIntegral(*it))
658                         continue;
659
660                 // core ist part from behind the scripts to the 'd'
661                 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("int")));
662
663                 // handle scripts if available
664                 if (!testIntSymbol(*it)) {
665                         p->cell(2) = (*it)->asScriptInset()->down();
666                         p->cell(3) = (*it)->asScriptInset()->up();
667                 }
668                 p->cell(0) = MathData(buf, it + 1, jt);
669
670                 // use the "thing" behind the 'd' as differential
671                 MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end(), kind);
672
673                 // remove used parts
674                 ar.erase(it + 1, tt);
675                 *it = MathAtom(p.release());
676         }
677         //lyxerr << "\nIntegrals to: " << ar << endl;
678 }
679
680
681 bool testTermDelimiter(MathAtom const & at)
682 {
683         return testString(at, "+") || testString(at, "-");
684 }
685
686
687 // try to extract a "term", i.e., something delimited by '+' or '-'.
688 // returns position behind the term
689 MathData::iterator extractTerm(MathData & ar,
690         MathData::iterator pos, MathData::iterator last)
691 {
692         while (pos != last && !testTermDelimiter(*pos)) {
693                 ar.push_back(*pos);
694                 ++pos;
695         }
696         return pos;
697 }
698
699
700 //
701 // search sums
702 //
703
704
705 bool testEqualSign(MathAtom const & at)
706 {
707         return testString(at, "=");
708 }
709
710
711 bool testSumSymbol(MathAtom const & p)
712 {
713         return testSymbol(p, from_ascii("sum"));
714 }
715
716
717 bool testSum(MathAtom const & at)
718 {
719         return
720          testSumSymbol(at) ||
721                 ( at->asScriptInset()
722                   && at->asScriptInset()->nuc().size()
723                         && testSumSymbol(at->asScriptInset()->nuc().back()) );
724 }
725
726
727 // replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
728 // assume 'extractDelims' ran before
729 void extractSums(MathData & ar)
730 {
731         // we need at least two items...
732         if (ar.size() < 2)
733                 return;
734
735         Buffer * buf = ar.buffer();
736
737         //lyxerr << "\nSums from: " << ar << endl;
738         for (size_t i = 0; i + 1 < ar.size(); ++i) {
739                 MathData::iterator it = ar.begin() + i;
740
741                 // is this a sum name?
742                 if (!testSum(ar[i]))
743                         continue;
744
745                 // create a proper inset as replacement
746                 auto_ptr<InsetMathExInt> p(new InsetMathExInt(buf, from_ascii("sum")));
747
748                 // collect lower bound and summation index
749                 InsetMathScript const * sub = ar[i]->asScriptInset();
750                 if (sub && sub->hasDown()) {
751                         // try to figure out the summation index from the subscript
752                         MathData const & ar = sub->down();
753                         MathData::const_iterator xt =
754                                 find_if(ar.begin(), ar.end(), &testEqualSign);
755                         if (xt != ar.end()) {
756                                 // we found a '=', use everything in front of that as index,
757                                 // and everything behind as lower index
758                                 p->cell(1) = MathData(buf, ar.begin(), xt);
759                                 p->cell(2) = MathData(buf, xt + 1, ar.end());
760                         } else {
761                                 // use everything as summation index, don't use scripts.
762                                 p->cell(1) = ar;
763                         }
764                 }
765
766                 // collect upper bound
767                 if (sub && sub->hasUp())
768                         p->cell(3) = sub->up();
769
770                 // use something  behind the script as core
771                 MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
772
773                 // cleanup
774                 ar.erase(it + 1, tt);
775                 *it = MathAtom(p.release());
776         }
777         //lyxerr << "\nSums to: " << ar << endl;
778 }
779
780
781 //
782 // search differential stuff
783 //
784
785 // tests for 'd' or '\partial'
786 bool testDiffItem(MathAtom const & at)
787 {
788         if (testString(at, "d") || testSymbol(at, "partial"))
789                 return true;
790
791         // we may have d^n .../d and splitScripts() has not yet seen it
792         InsetMathScript const * sup = at->asScriptInset();
793         if (sup && !sup->hasDown() && sup->hasUp() && sup->nuc().size() == 1) {
794                 MathAtom const & ma = sup->nuc().front();
795                 return testString(ma, "d") || testSymbol(ma, "partial");
796         }
797         return false;
798 }
799
800
801 bool testDiffArray(MathData const & ar)
802 {
803         return ar.size() && testDiffItem(ar.front());
804 }
805
806
807 bool testDiffFrac(MathAtom const & at)
808 {
809         return
810                 at->asFracInset()
811                         && testDiffArray(at->asFracInset()->cell(0))
812                         && testDiffArray(at->asFracInset()->cell(1));
813 }
814
815
816 void extractDiff(MathData & ar)
817 {
818         Buffer * buf = ar.buffer();
819         //lyxerr << "\nDiffs from: " << ar << endl;
820         for (size_t i = 0; i < ar.size(); ++i) {
821                 MathData::iterator it = ar.begin() + i;
822
823                 // is this a "differential fraction"?
824                 if (!testDiffFrac(*it))
825                         continue;
826
827                 InsetMathFrac const * f = (*it)->asFracInset();
828                 if (!f) {
829                         lyxerr << "should not happen" << endl;
830                         continue;
831                 }
832
833                 // create a proper diff inset
834                 auto_ptr<InsetMathDiff> diff(new InsetMathDiff(buf));
835
836                 // collect function, let jt point behind last used item
837                 MathData::iterator jt = it + 1;
838                 //int n = 1;
839                 MathData numer(f->cell(0));
840                 splitScripts(numer);
841                 if (numer.size() > 1 && numer[1]->asScriptInset()) {
842                         // this is something like  d^n f(x) / d... or  d^n / d...
843                         // FIXME
844                         //n = 1;
845                         if (numer.size() > 2)
846                                 diff->cell(0) = MathData(buf, numer.begin() + 2, numer.end());
847                         else
848                                 jt = extractTerm(diff->cell(0), jt, ar.end());
849                 } else {
850                         // simply d f(x) / d... or  d/d...
851                         if (numer.size() > 1)
852                                 diff->cell(0) = MathData(buf, numer.begin() + 1, numer.end());
853                         else
854                                 jt = extractTerm(diff->cell(0), jt, ar.end());
855                 }
856
857                 // collect denominator parts
858                 MathData denom(f->cell(1));
859                 splitScripts(denom);
860                 for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
861                         // find the next 'd'
862                         MathData::iterator et
863                                 = find_if(dt + 1, denom.end(), &testDiffItem);
864
865                         // point before this
866                         MathData::iterator st = et - 1;
867                         InsetMathScript const * script = (*st)->asScriptInset();
868                         if (script && script->hasUp()) {
869                                 // things like   d.../dx^n
870                                 int mult = 1;
871                                 if (extractNumber(script->up(), mult)) {
872                                         //lyxerr << "mult: " << mult << endl;
873                                         for (int i = 0; i < mult; ++i)
874                                                 diff->addDer(MathData(buf, dt + 1, st));
875                                 }
876                         } else {
877                                 // just  d.../dx
878                                 diff->addDer(MathData(buf, dt + 1, et));
879                         }
880                         dt = et;
881                 }
882
883                 // cleanup
884                 ar.erase(it + 1, jt);
885                 *it = MathAtom(diff.release());
886         }
887         //lyxerr << "\nDiffs to: " << ar << endl;
888 }
889
890
891 //
892 // search limits
893 //
894
895
896 bool testRightArrow(MathAtom const & at)
897 {
898         return testSymbol(at, "to") || testSymbol(at, "rightarrow");
899 }
900
901
902
903 // replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
904 // assume 'extractDelims' ran before
905 void extractLims(MathData & ar)
906 {
907         Buffer * buf = ar.buffer();
908         //lyxerr << "\nLimits from: " << ar << endl;
909         for (size_t i = 0; i < ar.size(); ++i) {
910                 MathData::iterator it = ar.begin() + i;
911
912                 // must be a script inset with a subscript (without superscript)
913                 InsetMathScript const * sub = (*it)->asScriptInset();
914                 if (!sub || !sub->hasDown() || sub->hasUp() || sub->nuc().size() != 1)
915                         continue;
916
917                 // is this a limit function?
918                 if (!testSymbol(sub->nuc().front(), "lim"))
919                         continue;
920
921                 // subscript must contain a -> symbol
922                 MathData const & s = sub->down();
923                 MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
924                 if (st == s.end())
925                         continue;
926
927                 // the -> splits the subscript int x and x0
928                 MathData x  = MathData(buf, s.begin(), st);
929                 MathData x0 = MathData(buf, st + 1, s.end());
930
931                 // use something behind the script as core
932                 MathData f;
933                 MathData::iterator tt = extractTerm(f, it + 1, ar.end());
934
935                 // cleanup
936                 ar.erase(it + 1, tt);
937
938                 // create a proper inset as replacement
939                 *it = MathAtom(new InsetMathLim(buf, f, x, x0));
940         }
941         //lyxerr << "\nLimits to: " << ar << endl;
942 }
943
944
945 //
946 // combine searches
947 //
948
949 void extractStructure(MathData & ar, ExternalMath kind)
950 {
951         //lyxerr << "\nStructure from: " << ar << endl;
952         if (kind != MATHML && kind != HTML)
953                 splitScripts(ar);
954         extractDelims(ar);
955         extractIntegrals(ar, kind);
956         if (kind != MATHML && kind != HTML)
957                 extractSums(ar);
958         extractNumbers(ar);
959         extractMatrices(ar);
960         extractFunctions(ar, kind);
961         if (kind != MATHML && kind != HTML) {
962                 extractDets(ar);
963                 extractDiff(ar);
964                 extractExps(ar);
965                 extractLims(ar);
966                 extractStrings(ar);
967         }
968         //lyxerr << "\nStructure to: " << ar << endl;
969 }
970
971
972 namespace {
973
974         string captureOutput(string const & cmd, string const & data)
975         {
976                 // In order to avoid parsing problems with command interpreters
977                 // we pass input data through a file
978                 FileName const cas_tmpfile = FileName::tempName("casinput");
979                 if (cas_tmpfile.empty()) {
980                         lyxerr << "Warning: cannot create temporary file."
981                                << endl;
982                         return string();
983                 }
984                 ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
985                 os << data << endl;
986                 os.close();
987                 string command =  cmd + " < "
988                         + quoteName(cas_tmpfile.toFilesystemEncoding());
989                 lyxerr << "calling: " << cmd
990                        << "\ninput: '" << data << "'" << endl;
991                 cmd_ret const ret = runCommand(command);
992                 cas_tmpfile.removeFile();
993                 return ret.second;
994         }
995
996         size_t get_matching_brace(string const & str, size_t i)
997         {
998                 int count = 1;
999                 size_t n = str.size();
1000                 while (i < n) {
1001                         i = str.find_first_of("{}", i+1);
1002                         if (i == npos)
1003                                 return i;
1004                         if (str[i] == '{')
1005                                 ++count;
1006                         else
1007                                 --count;
1008                         if (count == 0)
1009                                 return i;
1010                 }
1011                 return npos;
1012         }
1013
1014         size_t get_matching_brace_back(string const & str, size_t i)
1015         {
1016                 int count = 1;
1017                 while (i > 0) {
1018                         i = str.find_last_of("{}", i-1);
1019                         if (i == npos)
1020                                 return i;
1021                         if (str[i] == '}')
1022                                 ++count;
1023                         else
1024                                 --count;
1025                         if (count == 0)
1026                                 return i;
1027                 }
1028                 return npos;
1029         }
1030
1031         MathData pipeThroughMaxima(docstring const &, MathData const & ar)
1032         {
1033                 odocstringstream os;
1034                 MaximaStream ms(os);
1035                 ms << ar;
1036                 docstring expr = os.str();
1037                 docstring const header = from_ascii("simpsum:true;");
1038
1039                 string out;
1040                 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1041                         // try to fix missing '*' the hard way
1042                         //
1043                         // > echo "2x;" | maxima
1044                         // ...
1045                         // (C1) Incorrect syntax: x is not an infix operator
1046                         // 2x;
1047                         //  ^
1048                         //
1049                         lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
1050                         docstring full = header + "tex(" + expr + ");";
1051                         out = captureOutput("maxima", to_utf8(full));
1052
1053                         // leave loop if expression syntax is probably ok
1054                         if (out.find("Incorrect syntax") == npos)
1055                                 break;
1056
1057                         // search line with "Incorrect syntax"
1058                         istringstream is(out);
1059                         string line;
1060                         while (is) {
1061                                 getline(is, line);
1062                                 if (line.find("Incorrect syntax") != npos)
1063                                         break;
1064                         }
1065
1066                         // 2nd next line is the one with caret
1067                         getline(is, line);
1068                         getline(is, line);
1069                         size_t pos = line.find('^');
1070                         lyxerr << "found caret at pos: '" << pos << "'" << endl;
1071                         if (pos == npos || pos < 4)
1072                                 break; // caret position not found
1073                         pos -= 4; // skip the "tex(" part
1074                         if (expr[pos] == '*')
1075                                 break; // two '*' in a row are definitely bad
1076                         expr.insert(pos, from_ascii("*"));
1077                 }
1078
1079                 vector<string> tmp = getVectorFromString(out, "$$");
1080                 if (tmp.size() < 2)
1081                         return MathData();
1082
1083                 out = subst(tmp[1], "\\>", string());
1084                 lyxerr << "output: '" << out << "'" << endl;
1085
1086                 // Ugly code that tries to make the result prettier
1087                 size_t i = out.find("\\mathchoice");
1088                 while (i != npos) {
1089                         size_t j = get_matching_brace(out, i + 12);
1090                         size_t k = get_matching_brace(out, j + 1);
1091                         k = get_matching_brace(out, k + 1);
1092                         k = get_matching_brace(out, k + 1);
1093                         string mid = out.substr(i + 13, j - i - 13);
1094                         if (mid.find("\\over") != npos)
1095                                 mid = '{' + mid + '}';
1096                         out = out.substr(0, i)
1097                                 + mid
1098                                 + out.substr(k + 1);
1099                         //lyxerr << "output: " << out << endl;
1100                         i = out.find("\\mathchoice", i);
1101                         break;
1102                 }
1103
1104                 i = out.find("\\over");
1105                 while (i != npos) {
1106                         size_t j = get_matching_brace_back(out, i - 1);
1107                         if (j == npos || j == 0)
1108                                 break;
1109                         size_t k = get_matching_brace(out, i + 5);
1110                         if (k == npos || k + 1 == out.size())
1111                                 break;
1112                         out = out.substr(0, j - 1)
1113                                 + "\\frac"
1114                                 + out.substr(j, i - j)
1115                                 + out.substr(i + 5, k - i - 4)
1116                                 + out.substr(k + 2);
1117                         //lyxerr << "output: " << out << endl;
1118                         i = out.find("\\over", i + 4);
1119                 }
1120                 MathData res;
1121                 mathed_parse_cell(res, from_utf8(out));
1122                 return res;
1123         }
1124
1125
1126         MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
1127         {
1128                 string header = "readlib(latex):\n";
1129
1130                 // remove the \\it for variable names
1131                 //"#`latex/csname_font` := `\\it `:"
1132                 header +=
1133                         "`latex/csname_font` := ``:\n";
1134
1135                 // export matrices in (...) instead of [...]
1136                 header +=
1137                         "`latex/latex/matrix` := "
1138                                 "subs(`[`=`(`, `]`=`)`,"
1139                                         "eval(`latex/latex/matrix`)):\n";
1140
1141                 // replace \\cdots with proper '*'
1142                 header +=
1143                         "`latex/latex/*` := "
1144                                 "subs(`\\,`=`\\cdot `,"
1145                                         "eval(`latex/latex/*`)):\n";
1146
1147                 // remove spurious \\noalign{\\medskip} in matrix output
1148                 header +=
1149                         "`latex/latex/matrix`:= "
1150                                 "subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
1151                                         "eval(`latex/latex/matrix`)):\n";
1152
1153                 //"#`latex/latex/symbol` "
1154                 //      " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
1155
1156                 string trailer = "quit;";
1157                 odocstringstream os;
1158                 MapleStream ms(os);
1159                 ms << ar;
1160                 string expr = to_utf8(os.str());
1161                 lyxerr << "ar: '" << ar << "'\n"
1162                        << "ms: '" << expr << "'" << endl;
1163
1164                 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1165                         // try to fix missing '*' the hard way by using mint
1166                         //
1167                         // ... > echo "1A;" | mint -i 1 -S -s -q
1168                         // on line     1: 1A;
1169                         //                 ^ syntax error -
1170                         //                   Probably missing an operator such as * p
1171                         //
1172                         lyxerr << "checking expr: '" << expr << "'" << endl;
1173                         string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
1174                         if (out.empty())
1175                                 break; // expression syntax is ok
1176                         istringstream is(out);
1177                         string line;
1178                         getline(is, line);
1179                         if (line.find("on line") != 0)
1180                                 break; // error message not identified
1181                         getline(is, line);
1182                         size_t pos = line.find('^');
1183                         if (pos == string::npos || pos < 15)
1184                                 break; // caret position not found
1185                         pos -= 15; // skip the "on line ..." part
1186                         if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
1187                                 break; // two '*' in a row are definitely bad
1188                         expr.insert(pos, 1, '*');
1189                 }
1190
1191                 // FIXME UNICODE Is utf8 encoding correct?
1192                 string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
1193                 string out = captureOutput("maple -q", header + full + trailer);
1194
1195                 // change \_ into _
1196
1197                 //
1198                 MathData res;
1199                 mathed_parse_cell(res, from_utf8(out));
1200                 return res;
1201         }
1202
1203
1204         MathData pipeThroughOctave(docstring const &, MathData const & ar)
1205         {
1206                 odocstringstream os;
1207                 OctaveStream vs(os);
1208                 vs << ar;
1209                 string expr = to_utf8(os.str());
1210                 string out;
1211                 // FIXME const cast
1212                 Buffer * buf = const_cast<Buffer *>(ar.buffer());
1213                 lyxerr << "pipe: ar: '" << ar << "'\n"
1214                        << "pipe: expr: '" << expr << "'" << endl;
1215
1216                 for (int i = 0; i < 100; ++i) { // at most 100 attempts
1217                         //
1218                         // try to fix missing '*' the hard way
1219                         // parse error:
1220                         // >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
1221                         //                                   ^
1222                         //
1223                         lyxerr << "checking expr: '" << expr << "'" << endl;
1224                         out = captureOutput("octave -q 2>&1", expr);
1225                         lyxerr << "output: '" << out << "'" << endl;
1226
1227                         // leave loop if expression syntax is probably ok
1228                         if (out.find("parse error:") == string::npos)
1229                                 break;
1230
1231                         // search line with single caret
1232                         istringstream is(out);
1233                         string line;
1234                         while (is) {
1235                                 getline(is, line);
1236                                 lyxerr << "skipping line: '" << line << "'" << endl;
1237                                 if (line.find(">>> ") != string::npos)
1238                                         break;
1239                         }
1240
1241                         // found line with error, next line is the one with caret
1242                         getline(is, line);
1243                         size_t pos = line.find('^');
1244                         lyxerr << "caret line: '" << line << "'" << endl;
1245                         lyxerr << "found caret at pos: '" << pos << "'" << endl;
1246                         if (pos == string::npos || pos < 4)
1247                                 break; // caret position not found
1248                         pos -= 4; // skip the ">>> " part
1249                         if (expr[pos] == '*')
1250                                 break; // two '*' in a row are definitely bad
1251                         expr.insert(pos, 1, '*');
1252                 }
1253
1254                 // remove 'ans = ' taking into account that there may be an
1255                 // ansi control sequence before, such as '\033[?1034hans = '
1256                 size_t i = out.find("ans = ");
1257                 if (i == string::npos)
1258                         return MathData();
1259                 out = out.substr(i + 6);
1260
1261                 // parse output as matrix or single number
1262                 MathAtom at(new InsetMathArray(buf, from_ascii("array"), from_utf8(out)));
1263                 InsetMathArray const * mat = at->asArrayInset();
1264                 MathData res(buf);
1265                 if (mat->ncols() == 1 && mat->nrows() == 1)
1266                         res.append(mat->cell(0));
1267                 else {
1268                         res.push_back(MathAtom(
1269                                 new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
1270                         res.back().nucleus()->cell(0).push_back(at);
1271                 }
1272                 return res;
1273         }
1274
1275
1276         string fromMathematicaName(string const & name)
1277         {
1278                 if (name == "Sin")    return "sin";
1279                 if (name == "Sinh")   return "sinh";
1280                 if (name == "ArcSin") return "arcsin";
1281                 if (name == "Cos")    return "cos";
1282                 if (name == "Cosh")   return "cosh";
1283                 if (name == "ArcCos") return "arccos";
1284                 if (name == "Tan")    return "tan";
1285                 if (name == "Tanh")   return "tanh";
1286                 if (name == "ArcTan") return "arctan";
1287                 if (name == "Cot")    return "cot";
1288                 if (name == "Coth")   return "coth";
1289                 if (name == "Csc")    return "csc";
1290                 if (name == "Sec")    return "sec";
1291                 if (name == "Exp")    return "exp";
1292                 if (name == "Log")    return "log";
1293                 if (name == "Arg" )   return "arg";
1294                 if (name == "Det" )   return "det";
1295                 if (name == "GCD" )   return "gcd";
1296                 if (name == "Max" )   return "max";
1297                 if (name == "Min" )   return "min";
1298                 if (name == "Erf" )   return "erf";
1299                 if (name == "Erfc" )  return "erfc";
1300                 return name;
1301         }
1302
1303
1304         void prettifyMathematicaOutput(string & out, string const & macroName,
1305                         bool roman, bool translate)
1306         {
1307                 string const macro = "\\" + macroName + "{";
1308                 size_t const len = macro.length();
1309                 size_t i = out.find(macro);
1310
1311                 while (i != npos) {
1312                         size_t const j = get_matching_brace(out, i + len);
1313                         string const name = out.substr(i + len, j - i - len);
1314                         out = out.substr(0, i)
1315                                 + (roman ? "\\mathrm{" : "")
1316                                 + (translate ? fromMathematicaName(name) : name)
1317                                 + out.substr(roman ? j : j + 1);
1318                         //lyxerr << "output: " << out << endl;
1319                         i = out.find(macro, i);
1320                 }
1321         }
1322
1323
1324         MathData pipeThroughMathematica(docstring const &, MathData const & ar)
1325         {
1326                 odocstringstream os;
1327                 MathematicaStream ms(os);
1328                 ms << ar;
1329                 // FIXME UNICODE Is utf8 encoding correct?
1330                 string const expr = to_utf8(os.str());
1331                 string out;
1332
1333                 lyxerr << "expr: '" << expr << "'" << endl;
1334
1335                 string const full = "TeXForm[" + expr + "]";
1336                 out = captureOutput("math", full);
1337                 lyxerr << "output: '" << out << "'" << endl;
1338
1339                 size_t pos1 = out.find("Out[1]//TeXForm= ");
1340                 size_t pos2 = out.find("In[2]:=");
1341
1342                 if (pos1 == string::npos || pos2 == string::npos)
1343                         return MathData();
1344
1345                 // get everything from pos1+17 to pos2
1346                 out = out.substr(pos1 + 17, pos2 - pos1 - 17);
1347                 out = subst(subst(out, '\r', ' '), '\n', ' ');
1348
1349                 // tries to make the result prettier
1350                 prettifyMathematicaOutput(out, "Mfunction", true, true);
1351                 prettifyMathematicaOutput(out, "Muserfunction", true, false);
1352                 prettifyMathematicaOutput(out, "Mvariable", false, false);
1353
1354                 MathData res;
1355                 mathed_parse_cell(res, from_utf8(out));
1356                 return res;
1357         }
1358
1359 }
1360
1361 } // anon namespace
1362
1363 void write(MathData const & dat, WriteStream & wi)
1364 {
1365         MathData ar = dat;
1366         extractStrings(ar);
1367         wi.firstitem() = true;
1368         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it) {
1369                 (*it)->write(wi);
1370                 wi.firstitem() = false;
1371         }
1372 }
1373
1374
1375 void normalize(MathData const & ar, NormalStream & os)
1376 {
1377         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1378                 (*it)->normalize(os);
1379 }
1380
1381
1382 void octave(MathData const & dat, OctaveStream & os)
1383 {
1384         MathData ar = dat;
1385         extractStructure(ar, OCTAVE);
1386         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1387                 (*it)->octave(os);
1388 }
1389
1390
1391 void maple(MathData const & dat, MapleStream & os)
1392 {
1393         MathData ar = dat;
1394         extractStructure(ar, MAPLE);
1395         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1396                 (*it)->maple(os);
1397 }
1398
1399
1400 void maxima(MathData const & dat, MaximaStream & os)
1401 {
1402         MathData ar = dat;
1403         extractStructure(ar, MAXIMA);
1404         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1405                 (*it)->maxima(os);
1406 }
1407
1408
1409 void mathematica(MathData const & dat, MathematicaStream & os)
1410 {
1411         MathData ar = dat;
1412         extractStructure(ar, MATHEMATICA);
1413         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1414                 (*it)->mathematica(os);
1415 }
1416
1417
1418 void mathmlize(MathData const & dat, MathStream & os)
1419 {
1420         MathData ar = dat;
1421         extractStructure(ar, MATHML);
1422         if (ar.size() == 0) {
1423                 if (!os.inText())
1424                         os << "<mrow/>";
1425         } else if (ar.size() == 1)
1426                 os << ar.front();
1427         else {
1428                 if (!os.inText())
1429                         os << MTag("mrow");
1430                 for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1431                         (*it)->mathmlize(os);
1432                 if (!os.inText())
1433                         os << ETag("mrow");
1434         }
1435 }
1436
1437
1438 void htmlize(MathData const & dat, HtmlStream & os)
1439 {
1440         MathData ar = dat;
1441         extractStructure(ar, HTML);
1442         if (ar.size() == 0) 
1443                 return;
1444         if (ar.size() == 1) {
1445                 os << ar.front();
1446                 return;
1447         }
1448         for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
1449                 (*it)->htmlize(os);
1450 }
1451
1452
1453 // convert this inset somehow to a number
1454 bool extractNumber(MathData const & ar, int & i)
1455 {
1456         idocstringstream is(charSequence(ar.begin(), ar.end()));
1457         is >> i;
1458         return is;
1459 }
1460
1461
1462 bool extractNumber(MathData const & ar, double & d)
1463 {
1464         idocstringstream is(charSequence(ar.begin(), ar.end()));
1465         is >> d;
1466         return is;
1467 }
1468
1469
1470 MathData pipeThroughExtern(string const & lang, docstring const & extra,
1471         MathData const & ar)
1472 {
1473         if (lang == "octave")
1474                 return pipeThroughOctave(extra, ar);
1475
1476         if (lang == "maxima")
1477                 return pipeThroughMaxima(extra, ar);
1478
1479         if (lang == "maple")
1480                 return pipeThroughMaple(extra, ar);
1481
1482         if (lang == "mathematica")
1483                 return pipeThroughMathematica(extra, ar);
1484
1485         // create normalized expression
1486         odocstringstream os;
1487         NormalStream ns(os);
1488         os << '[' << extra << ' ';
1489         ns << ar;
1490         os << ']';
1491         // FIXME UNICODE Is utf8 encoding correct?
1492         string data = to_utf8(os.str());
1493
1494         // search external script
1495         FileName const file = libFileSearch("mathed", "extern_" + lang);
1496         if (file.empty()) {
1497                 lyxerr << "converter to '" << lang << "' not found" << endl;
1498                 return MathData();
1499         }
1500
1501         // run external sript
1502         string out = captureOutput(file.absFilename(), data);
1503         MathData res;
1504         mathed_parse_cell(res, from_utf8(out));
1505         return res;
1506 }
1507
1508
1509 } // namespace lyx