2003-08-19 13:00:56 +00:00
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
* \file MathExtern.cpp
|
2003-08-19 13:00:56 +00:00
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
*
|
2008-11-14 15:58:50 +00:00
|
|
|
* \author André Pönitz
|
2003-08-19 13:00:56 +00:00
|
|
|
*
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
*/
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
// This file contains most of the magic that extracts "context
|
2007-10-04 17:35:27 +00:00
|
|
|
// information" from the unstructered layout-oriented stuff in
|
2007-04-26 16:05:57 +00:00
|
|
|
// MathData.
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
#include <config.h>
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "MathExtern.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
|
2009-12-16 17:01:09 +00:00
|
|
|
#include "InsetMathAMSArray.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "InsetMathArray.h"
|
|
|
|
#include "InsetMathChar.h"
|
|
|
|
#include "InsetMathDelim.h"
|
|
|
|
#include "InsetMathDiff.h"
|
|
|
|
#include "InsetMathExFunc.h"
|
|
|
|
#include "InsetMathExInt.h"
|
|
|
|
#include "InsetMathFont.h"
|
|
|
|
#include "InsetMathFrac.h"
|
|
|
|
#include "InsetMathLim.h"
|
|
|
|
#include "InsetMathMatrix.h"
|
|
|
|
#include "InsetMathNumber.h"
|
|
|
|
#include "InsetMathScript.h"
|
|
|
|
#include "InsetMathString.h"
|
|
|
|
#include "InsetMathSymbol.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
#include "MathData.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
#include "MathParser.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
#include "MathStream.h"
|
|
|
|
|
2015-10-12 22:31:15 +00:00
|
|
|
#include "Encoding.h"
|
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-11-05 23:46:17 +00:00
|
|
|
#include "support/docstream.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
#include "support/FileName.h"
|
2002-06-18 15:44:30 +00:00
|
|
|
#include "support/filetools.h"
|
2015-10-12 22:31:15 +00:00
|
|
|
#include "support/gettext.h"
|
2002-10-28 17:15:19 +00:00
|
|
|
#include "support/lstrings.h"
|
2014-06-09 11:05:50 +00:00
|
|
|
#include "support/TempFile.h"
|
2015-10-12 22:31:15 +00:00
|
|
|
#include "support/textutils.h"
|
2016-06-02 20:58:52 +00:00
|
|
|
#include "support/unique_ptr.h"
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2003-09-05 12:54:37 +00:00
|
|
|
#include <algorithm>
|
2004-07-24 10:55:30 +00:00
|
|
|
#include <sstream>
|
2007-01-16 18:10:10 +00:00
|
|
|
#include <fstream>
|
2008-02-07 17:04:06 +00:00
|
|
|
#include <memory>
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2007-12-12 18:57:56 +00:00
|
|
|
using namespace lyx::support;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
namespace lyx {
|
|
|
|
|
2009-11-14 20:20:17 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum ExternalMath {
|
2010-03-29 22:52:13 +00:00
|
|
|
HTML,
|
2009-11-14 20:20:17 +00:00
|
|
|
MAPLE,
|
|
|
|
MAXIMA,
|
|
|
|
MATHEMATICA,
|
|
|
|
MATHML,
|
|
|
|
OCTAVE
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-10-04 17:35:27 +00:00
|
|
|
static char const * function_names[] = {
|
|
|
|
"arccos", "arcsin", "arctan", "arg", "bmod",
|
|
|
|
"cos", "cosh", "cot", "coth", "csc", "deg",
|
|
|
|
"det", "dim", "exp", "gcd", "hom", "inf", "ker",
|
|
|
|
"lg", "lim", "liminf", "limsup", "ln", "log",
|
|
|
|
"max", "min", "sec", "sin", "sinh", "sup",
|
|
|
|
"tan", "tanh", "Pr", 0
|
|
|
|
};
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
static size_t const npos = lyx::docstring::npos;
|
2002-02-16 15:59:55 +00:00
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// define a function for tests
|
2002-08-08 16:44:06 +00:00
|
|
|
typedef bool TestItemFunc(MathAtom const &);
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// define a function for replacing subexpressions
|
2007-04-26 16:05:57 +00:00
|
|
|
typedef MathAtom ReplaceArgumentFunc(const MathData & ar);
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
|
2009-12-17 17:42:21 +00:00
|
|
|
|
2002-03-21 17:42:56 +00:00
|
|
|
// try to extract a super/subscript
|
2001-11-15 14:14:37 +00:00
|
|
|
// modify iterator position to point behind the thing
|
2007-04-26 16:05:57 +00:00
|
|
|
bool extractScript(MathData & ar,
|
|
|
|
MathData::iterator & pos, MathData::iterator last, bool superscript)
|
2001-11-15 14:14:37 +00:00
|
|
|
{
|
|
|
|
// nothing to get here
|
|
|
|
if (pos == last)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// is this a scriptinset?
|
|
|
|
if (!(*pos)->asScriptInset())
|
|
|
|
return false;
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// do we want superscripts only?
|
|
|
|
if (superscript && !(*pos)->asScriptInset()->hasUp())
|
|
|
|
return false;
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// it is a scriptinset, use it.
|
|
|
|
ar.push_back(*pos);
|
|
|
|
++pos;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// try to extract an "argument" to some function.
|
|
|
|
// returns position behind the argument
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator extractArgument(MathData & ar,
|
2009-11-14 20:20:17 +00:00
|
|
|
MathData::iterator pos, MathData::iterator last,
|
|
|
|
ExternalMath kind, bool function = false)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
|
|
|
// nothing to get here
|
|
|
|
if (pos == last)
|
|
|
|
return pos;
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// something delimited _is_ an argument
|
2001-11-13 18:33:48 +00:00
|
|
|
if ((*pos)->asDelimInset()) {
|
2006-09-02 01:57:36 +00:00
|
|
|
// leave out delimiters if this is a function argument
|
2009-12-17 17:52:09 +00:00
|
|
|
// unless we are doing MathML, in which case we do want
|
|
|
|
// the delimiters
|
2010-03-29 22:52:13 +00:00
|
|
|
if (function && kind != MATHML && kind != HTML) {
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData const & arg = (*pos)->asDelimInset()->cell(0);
|
|
|
|
MathData::const_iterator cur = arg.begin();
|
|
|
|
MathData::const_iterator end = arg.end();
|
2006-09-02 01:57:36 +00:00
|
|
|
while (cur != end)
|
|
|
|
ar.push_back(*cur++);
|
|
|
|
} else
|
|
|
|
ar.push_back(*pos);
|
|
|
|
++pos;
|
|
|
|
if (pos == last)
|
|
|
|
return pos;
|
|
|
|
// if there's one, get following superscript only if this
|
|
|
|
// isn't a function argument
|
|
|
|
if (!function)
|
|
|
|
extractScript(ar, pos, last, true);
|
|
|
|
return pos;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// always take the first thing, no matter what it is
|
|
|
|
ar.push_back(*pos);
|
|
|
|
|
|
|
|
// go ahead if possible
|
|
|
|
++pos;
|
|
|
|
if (pos == last)
|
|
|
|
return pos;
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// if the next item is a super/subscript, it most certainly belongs
|
|
|
|
// to the thing we have
|
|
|
|
extractScript(ar, pos, last, false);
|
2001-11-15 14:14:37 +00:00
|
|
|
if (pos == last)
|
|
|
|
return pos;
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// but it might be more than that.
|
|
|
|
// FIXME: not implemented
|
2007-04-26 16:05:57 +00:00
|
|
|
//for (MathData::iterator it = pos + 1; it != last; ++it) {
|
2001-11-13 18:33:48 +00:00
|
|
|
// // always take the first thing, no matter
|
|
|
|
// if (it == pos) {
|
|
|
|
// ar.push_back(*it);
|
|
|
|
// continue;
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
// returns sequence of char with same code starting at it up to end
|
|
|
|
// it might be less, though...
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring charSequence
|
2007-04-26 16:05:57 +00:00
|
|
|
(MathData::const_iterator it, MathData::const_iterator end)
|
2001-11-15 14:14:37 +00:00
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring s;
|
2002-05-30 07:09:54 +00:00
|
|
|
for (; it != end && (*it)->asCharInset(); ++it)
|
|
|
|
s += (*it)->getChar();
|
2002-06-18 15:44:30 +00:00
|
|
|
return s;
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractStrings(MathData & ar)
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nStrings from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
2002-06-18 15:44:30 +00:00
|
|
|
if (!ar[i]->asCharInset())
|
2001-11-15 14:14:37 +00:00
|
|
|
continue;
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring s = charSequence(ar.begin() + i, ar.end());
|
2006-09-16 18:11:38 +00:00
|
|
|
ar[i] = MathAtom(new InsetMathString(s));
|
2002-06-18 15:44:30 +00:00
|
|
|
ar.erase(i + 1, i + s.size());
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nStrings to: " << ar << endl;
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractMatrices(MathData & ar)
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nMatrices from: " << ar << endl;
|
2002-04-24 17:07:42 +00:00
|
|
|
// first pass for explicitly delimited stuff
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
2009-12-16 17:01:09 +00:00
|
|
|
InsetMathDelim const * const inset = ar[i]->asDelimInset();
|
|
|
|
if (!inset)
|
2002-08-09 10:22:35 +00:00
|
|
|
continue;
|
2009-12-16 17:01:09 +00:00
|
|
|
MathData const & arr = inset->cell(0);
|
2002-08-09 10:22:35 +00:00
|
|
|
if (arr.size() != 1)
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!arr.front()->asGridInset())
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
2009-12-16 17:01:09 +00:00
|
|
|
ar[i] = MathAtom(new InsetMathMatrix(*(arr.front()->asGridInset()),
|
|
|
|
inset->left_, inset->right_));
|
2002-04-24 17:07:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// second pass for AMS "pmatrix" etc
|
2009-12-16 17:01:09 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
|
|
|
InsetMathAMSArray const * const inset = ar[i]->asAMSArrayInset();
|
|
|
|
if (inset) {
|
|
|
|
string left = inset->name_left();
|
|
|
|
if (left == "Vert")
|
|
|
|
left = "[";
|
|
|
|
string right = inset->name_right();
|
|
|
|
if (right == "Vert")
|
|
|
|
right = "]";
|
|
|
|
ar[i] = MathAtom(new InsetMathMatrix(*inset, from_ascii(left), from_ascii(right)));
|
|
|
|
}
|
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nMatrices to: " << ar << endl;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2001-11-09 12:01:10 +00:00
|
|
|
// convert this inset somehow to a string
|
2006-10-22 10:15:23 +00:00
|
|
|
bool extractString(MathAtom const & at, docstring & str)
|
2001-11-09 12:01:10 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
if (at->getChar()) {
|
2006-10-22 10:15:23 +00:00
|
|
|
str = docstring(1, at->getChar());
|
2001-11-15 14:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2002-08-08 16:44:06 +00:00
|
|
|
if (at->asStringInset()) {
|
|
|
|
str = at->asStringInset()->str();
|
2001-11-15 14:14:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// is this a known function?
|
2006-10-22 10:15:23 +00:00
|
|
|
bool isKnownFunction(docstring const & str)
|
2006-09-02 01:57:36 +00:00
|
|
|
{
|
2007-10-04 17:35:27 +00:00
|
|
|
for (int i = 0; function_names[i]; ++i) {
|
2006-09-02 01:57:36 +00:00
|
|
|
if (str == function_names[i])
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// extract a function name from this inset
|
2006-10-22 10:15:23 +00:00
|
|
|
bool extractFunctionName(MathAtom const & at, docstring & str)
|
2006-09-02 01:57:36 +00:00
|
|
|
{
|
|
|
|
if (at->asSymbolInset()) {
|
|
|
|
str = at->asSymbolInset()->name();
|
|
|
|
return isKnownFunction(str);
|
|
|
|
}
|
|
|
|
if (at->asUnknownInset()) {
|
|
|
|
// assume it is well known...
|
|
|
|
str = at->name();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (at->asFontInset() && at->name() == "mathrm") {
|
|
|
|
// assume it is well known...
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData const & ar = at->asFontInset()->cell(0);
|
2006-09-02 01:57:36 +00:00
|
|
|
str = charSequence(ar.begin(), ar.end());
|
|
|
|
return ar.size() == str.size();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
bool testString(MathAtom const & at, docstring const & str)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring s;
|
2002-08-08 16:44:06 +00:00
|
|
|
return extractString(at, s) && str == s;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
2001-11-09 14:23:44 +00:00
|
|
|
|
2001-11-12 14:37:43 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
bool testString(MathAtom const & at, char const * const str)
|
|
|
|
{
|
|
|
|
return testString(at, from_ascii(str));
|
|
|
|
}
|
|
|
|
|
2001-11-09 14:23:44 +00:00
|
|
|
// search end of nested sequence
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator endNestSearch(
|
|
|
|
MathData::iterator it,
|
|
|
|
MathData::iterator last,
|
2001-11-09 14:23:44 +00:00
|
|
|
TestItemFunc testOpen,
|
|
|
|
TestItemFunc testClose
|
|
|
|
)
|
|
|
|
{
|
|
|
|
for (int level = 0; it != last; ++it) {
|
2002-08-08 16:44:06 +00:00
|
|
|
if (testOpen(*it))
|
2001-11-09 14:23:44 +00:00
|
|
|
++level;
|
2002-08-08 16:44:06 +00:00
|
|
|
if (testClose(*it))
|
2001-11-09 14:23:44 +00:00
|
|
|
--level;
|
|
|
|
if (level == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// replace nested sequences by a real Insets
|
|
|
|
void replaceNested(
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData & ar,
|
2001-11-09 14:23:44 +00:00
|
|
|
TestItemFunc testOpen,
|
|
|
|
TestItemFunc testClose,
|
2009-11-08 11:45:46 +00:00
|
|
|
ReplaceArgumentFunc replaceArg)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
2001-11-09 10:44:24 +00:00
|
|
|
// use indices rather than iterators for the loop because we are going
|
|
|
|
// to modify the array.
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
2001-11-09 14:23:44 +00:00
|
|
|
// check whether this is the begin of the sequence
|
2002-08-09 10:22:35 +00:00
|
|
|
if (!testOpen(ar[i]))
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
|
|
|
|
2001-11-09 14:23:44 +00:00
|
|
|
// search end of sequence
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator it = ar.begin() + i;
|
|
|
|
MathData::iterator jt = endNestSearch(it, ar.end(), testOpen, testClose);
|
2001-11-09 12:01:10 +00:00
|
|
|
if (jt == ar.end())
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// replace the original stuff by the new inset
|
2009-11-08 11:45:46 +00:00
|
|
|
ar[i] = replaceArg(MathData(buf, it + 1, jt));
|
2002-10-28 09:21:40 +00:00
|
|
|
ar.erase(it + 1, jt + 1);
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2002-03-21 17:42:56 +00:00
|
|
|
}
|
2001-11-09 10:44:24 +00:00
|
|
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// split scripts into seperate super- and subscript insets. sub goes in
|
2002-03-21 17:42:56 +00:00
|
|
|
// front of super...
|
2001-11-15 14:14:37 +00:00
|
|
|
//
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
void splitScripts(MathData & ar)
|
2001-11-15 14:14:37 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nScripts from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathScript const * script = ar[i]->asScriptInset();
|
2001-11-15 14:14:37 +00:00
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// is this a script inset and do we also have a superscript?
|
|
|
|
if (!script || !script->hasUp())
|
2002-08-09 10:22:35 +00:00
|
|
|
continue;
|
2006-09-02 01:57:36 +00:00
|
|
|
|
|
|
|
// we must have a nucleus if we only have a superscript
|
2012-10-21 19:14:16 +00:00
|
|
|
if (!script->hasDown() && script->nuc().empty())
|
2001-11-15 14:14:37 +00:00
|
|
|
continue;
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
if (script->nuc().size() == 1) {
|
|
|
|
// leave alone sums and integrals
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathSymbol const * sym =
|
2006-09-02 01:57:36 +00:00
|
|
|
script->nuc().front()->asSymbolInset();
|
2009-12-17 17:42:21 +00:00
|
|
|
if (sym && (sym->name() == "sum" || sym->name() == "int"))
|
2006-09-02 01:57:36 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// create extra script inset and move superscript over
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathScript * p = ar[i].nucleus()->asScriptInset();
|
2016-06-02 20:58:52 +00:00
|
|
|
auto q = make_unique<InsetMathScript>(buf, true);
|
2004-01-28 16:21:29 +00:00
|
|
|
swap(q->up(), p->up());
|
2001-11-15 14:14:37 +00:00
|
|
|
p->removeScript(true);
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// if we don't have a subscript, get rid of the ScriptInset
|
|
|
|
if (!script->hasDown()) {
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData arg(p->nuc());
|
|
|
|
MathData::const_iterator it = arg.begin();
|
|
|
|
MathData::const_iterator et = arg.end();
|
2006-09-02 01:57:36 +00:00
|
|
|
ar.erase(i);
|
|
|
|
while (it != et)
|
|
|
|
ar.insert(i++, *it++);
|
|
|
|
} else
|
|
|
|
++i;
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// insert new inset behind
|
2003-11-03 17:47:28 +00:00
|
|
|
ar.insert(i, MathAtom(q.release()));
|
2001-11-15 14:14:37 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nScripts to: " << ar << endl;
|
2001-11-15 14:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-15 15:14:27 +00:00
|
|
|
//
|
|
|
|
// extract exp(...)
|
|
|
|
//
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractExps(MathData & ar)
|
2001-11-15 15:14:27 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nExps from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i + 1 < ar.size(); ++i) {
|
2001-11-15 15:14:27 +00:00
|
|
|
// is this 'e'?
|
2002-08-09 10:22:35 +00:00
|
|
|
if (ar[i]->getChar() != 'e')
|
2001-11-15 15:14:27 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// we need an exponent but no subscript
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathScript const * sup = ar[i + 1]->asScriptInset();
|
2001-11-15 15:14:27 +00:00
|
|
|
if (!sup || sup->hasDown())
|
|
|
|
continue;
|
|
|
|
|
2002-11-04 02:12:42 +00:00
|
|
|
// create a proper exp-inset as replacement
|
2009-11-08 11:45:46 +00:00
|
|
|
ar[i] = MathAtom(new InsetMathExFunc(buf, from_ascii("exp"), sup->cell(1)));
|
2002-08-09 10:22:35 +00:00
|
|
|
ar.erase(i + 1);
|
2001-11-15 15:14:27 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nExps to: " << ar << endl;
|
2001-11-15 15:14:27 +00:00
|
|
|
}
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
|
2002-06-25 13:19:50 +00:00
|
|
|
//
|
|
|
|
// extract det(...) from |matrix|
|
|
|
|
//
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractDets(MathData & ar)
|
2002-06-25 13:19:50 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\ndet from: " << ar << endl;
|
2007-04-26 16:05:57 +00:00
|
|
|
for (MathData::iterator it = ar.begin(); it != ar.end(); ++it) {
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathDelim const * del = (*it)->asDelimInset();
|
2002-06-25 13:19:50 +00:00
|
|
|
if (!del)
|
|
|
|
continue;
|
|
|
|
if (!del->isAbs())
|
|
|
|
continue;
|
2009-11-08 11:45:46 +00:00
|
|
|
*it = MathAtom(new InsetMathExFunc(buf, from_ascii("det"), del->cell(0)));
|
2002-06-25 13:19:50 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\ndet to: " << ar << endl;
|
2002-06-25 13:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
//
|
|
|
|
// search numbers
|
|
|
|
//
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
bool isDigitOrSimilar(char_type c)
|
2002-06-18 15:44:30 +00:00
|
|
|
{
|
|
|
|
return ('0' <= c && c <= '9') || c == '.';
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns sequence of digits
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring digitSequence
|
2007-04-26 16:05:57 +00:00
|
|
|
(MathData::const_iterator it, MathData::const_iterator end)
|
2002-06-18 15:44:30 +00:00
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring s;
|
2002-06-18 15:44:30 +00:00
|
|
|
for (; it != end && (*it)->asCharInset(); ++it) {
|
|
|
|
if (!isDigitOrSimilar((*it)->getChar()))
|
|
|
|
break;
|
|
|
|
s += (*it)->getChar();
|
|
|
|
}
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractNumbers(MathData & ar)
|
2002-06-18 15:44:30 +00:00
|
|
|
{
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nNumbers from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
2002-06-18 15:44:30 +00:00
|
|
|
if (!ar[i]->asCharInset())
|
|
|
|
continue;
|
|
|
|
if (!isDigitOrSimilar(ar[i]->asCharInset()->getChar()))
|
|
|
|
continue;
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring s = digitSequence(ar.begin() + i, ar.end());
|
2002-06-18 15:44:30 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
ar[i] = MathAtom(new InsetMathNumber(s));
|
2002-06-18 15:44:30 +00:00
|
|
|
ar.erase(i + 1, i + s.size());
|
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nNumbers to: " << ar << endl;
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-11-09 14:48:57 +00:00
|
|
|
//
|
2006-09-02 01:57:36 +00:00
|
|
|
// search delimiters
|
2001-11-09 14:48:57 +00:00
|
|
|
//
|
|
|
|
|
2005-04-10 14:08:54 +00:00
|
|
|
bool testOpenParen(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, "(");
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-10 14:08:54 +00:00
|
|
|
bool testCloseParen(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, ")");
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathAtom replaceParenDelims(const MathData & ar)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
|
|
|
|
from_ascii("("), from_ascii(")"), ar));
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
bool testOpenBracket(MathAtom const & at)
|
|
|
|
{
|
|
|
|
return testString(at, "[");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool testCloseBracket(MathAtom const & at)
|
|
|
|
{
|
|
|
|
return testString(at, "]");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathAtom replaceBracketDelims(const MathData & ar)
|
2006-09-02 01:57:36 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
return MathAtom(new InsetMathDelim(const_cast<Buffer *>(ar.buffer()),
|
|
|
|
from_ascii("["), from_ascii("]"), ar));
|
2006-09-02 01:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
// replace '('...')' and '['...']' sequences by a real InsetMathDelim
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractDelims(MathData & ar)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nDelims from: " << ar << endl;
|
2006-09-02 01:57:36 +00:00
|
|
|
replaceNested(ar, testOpenParen, testCloseParen, replaceParenDelims);
|
|
|
|
replaceNested(ar, testOpenBracket, testCloseBracket, replaceBracketDelims);
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nDelims to: " << ar << endl;
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-09 14:48:57 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// search well-known functions
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
// replace 'f' '(...)' and 'f' '^n' '(...)' sequences by a real InsetMathExFunc
|
2001-11-09 10:44:24 +00:00
|
|
|
// assume 'extractDelims' ran before
|
2009-11-14 20:20:17 +00:00
|
|
|
void extractFunctions(MathData & ar, ExternalMath kind)
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
2013-03-27 16:01:44 +00:00
|
|
|
// FIXME From what I can see, this is quite broken right now, for reasons
|
|
|
|
// I will note below. (RGH)
|
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
// we need at least two items...
|
2002-07-12 11:21:21 +00:00
|
|
|
if (ar.size() < 2)
|
2001-11-09 10:44:24 +00:00
|
|
|
return;
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nFunctions from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i + 1 < ar.size(); ++i) {
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator it = ar.begin() + i;
|
|
|
|
MathData::iterator jt = it + 1;
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring name;
|
2001-11-15 14:14:37 +00:00
|
|
|
// is it a function?
|
2006-09-02 01:57:36 +00:00
|
|
|
// it certainly is if it is well known...
|
2013-03-27 16:01:44 +00:00
|
|
|
|
|
|
|
// FIXME This will never give us anything. When we get here, *it will
|
|
|
|
// never point at a string, but only at a character. I.e., if we are
|
|
|
|
// working on "sin(x)", then we are seeing:
|
|
|
|
// [char s mathalpha][char i mathalpha][char n mathalpha][delim ( ) [char x mathalpha]]
|
|
|
|
// and of course we will not find the function name "sin" in there, but
|
|
|
|
// rather "n(x)".
|
|
|
|
//
|
|
|
|
// It appears that we original ran extractStrings() before we ran
|
|
|
|
// extractFunctions(), but Andre changed this at f200be55, I think
|
|
|
|
// because this messed up what he was trying to do with "dx" in the
|
|
|
|
// context of integrals.
|
|
|
|
//
|
|
|
|
// This could be fixed by looking at a charSequence instead of just at
|
|
|
|
// the various characters, one by one. But I am not sure I understand
|
|
|
|
// exactly what we are trying to do here. And it involves a lot of
|
|
|
|
// guessing.
|
2006-09-02 01:57:36 +00:00
|
|
|
if (!extractFunctionName(*it, name)) {
|
2001-11-15 09:51:57 +00:00
|
|
|
// is this a user defined function?
|
2013-03-27 16:01:44 +00:00
|
|
|
// probably not, if it doesn't have a name.
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!extractString(*it, name))
|
2001-11-09 10:44:24 +00:00
|
|
|
continue;
|
2001-11-15 14:14:37 +00:00
|
|
|
// it is not if it has no argument
|
2001-11-09 10:44:24 +00:00
|
|
|
if (jt == ar.end())
|
|
|
|
continue;
|
2001-11-15 14:14:37 +00:00
|
|
|
// guess so, if this is followed by
|
|
|
|
// a DelimInset with a single item in the cell
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathDelim const * del = (*jt)->asDelimInset();
|
2001-11-15 14:14:37 +00:00
|
|
|
if (!del || del->cell(0).size() != 1)
|
|
|
|
continue;
|
2013-03-27 16:01:44 +00:00
|
|
|
// fall through into main branch
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// do we have an exponent like in
|
|
|
|
// 'sin' '^2' 'x' -> 'sin(x)' '^2'
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData exp;
|
2006-09-02 01:57:36 +00:00
|
|
|
extractScript(exp, jt, ar.end(), true);
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// create a proper inset as replacement
|
2016-06-02 20:58:52 +00:00
|
|
|
auto p = make_unique<InsetMathExFunc>(buf, name);
|
2001-11-09 10:44:24 +00:00
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// jt points to the "argument". Get hold of this.
|
2009-11-14 20:20:17 +00:00
|
|
|
MathData::iterator st =
|
|
|
|
extractArgument(p->cell(0), jt, ar.end(), kind, true);
|
2001-11-09 10:44:24 +00:00
|
|
|
|
|
|
|
// replace the function name by a real function inset
|
2003-11-03 17:47:28 +00:00
|
|
|
*it = MathAtom(p.release());
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2001-11-09 10:44:24 +00:00
|
|
|
// remove the source of the argument from the array
|
2001-11-15 14:14:37 +00:00
|
|
|
ar.erase(it + 1, st);
|
|
|
|
|
|
|
|
// re-insert exponent
|
|
|
|
ar.insert(i + 1, exp);
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nFunctions to: " << ar << endl;
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
2002-03-21 17:42:56 +00:00
|
|
|
}
|
2001-11-09 10:44:24 +00:00
|
|
|
|
|
|
|
|
2001-11-09 14:48:57 +00:00
|
|
|
//
|
|
|
|
// search integrals
|
|
|
|
//
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
bool testSymbol(MathAtom const & at, docstring const & name)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return at->asSymbolInset() && at->asSymbolInset()->name() == name;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
bool testSymbol(MathAtom const & at, char const * const name)
|
|
|
|
{
|
|
|
|
return at->asSymbolInset() && at->asSymbolInset()->name() == from_ascii(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testIntSymbol(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
return testSymbol(at, from_ascii("int"));
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testIntegral(MathAtom const & at)
|
2002-07-30 17:51:19 +00:00
|
|
|
{
|
|
|
|
return
|
2002-08-08 16:44:06 +00:00
|
|
|
testIntSymbol(at) ||
|
2002-11-04 02:12:42 +00:00
|
|
|
( at->asScriptInset()
|
2012-10-21 19:14:16 +00:00
|
|
|
&& !at->asScriptInset()->nuc().empty()
|
2002-08-08 16:44:06 +00:00
|
|
|
&& testIntSymbol(at->asScriptInset()->nuc().back()) );
|
2002-07-30 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testIntDiff(MathAtom const & at)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, "d");
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
// replace '\int' ['_^'] x 'd''x'(...)' sequences by a real InsetMathExInt
|
2001-11-09 14:23:44 +00:00
|
|
|
// assume 'extractDelims' ran before
|
2009-11-14 20:20:17 +00:00
|
|
|
void extractIntegrals(MathData & ar, ExternalMath kind)
|
2001-11-09 14:23:44 +00:00
|
|
|
{
|
|
|
|
// we need at least three items...
|
2002-07-12 11:21:21 +00:00
|
|
|
if (ar.size() < 3)
|
2001-11-09 14:23:44 +00:00
|
|
|
return;
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nIntegrals from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i + 1 < ar.size(); ++i) {
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator it = ar.begin() + i;
|
2001-11-09 14:23:44 +00:00
|
|
|
|
|
|
|
// search 'd'
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator jt =
|
2002-07-30 17:51:19 +00:00
|
|
|
endNestSearch(it, ar.end(), testIntegral, testIntDiff);
|
2001-11-09 14:23:44 +00:00
|
|
|
|
2001-11-09 18:02:20 +00:00
|
|
|
// something sensible found?
|
|
|
|
if (jt == ar.end())
|
|
|
|
continue;
|
|
|
|
|
2002-07-30 17:51:19 +00:00
|
|
|
// is this a integral name?
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!testIntegral(*it))
|
2002-07-30 17:51:19 +00:00
|
|
|
continue;
|
2001-11-09 16:27:44 +00:00
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// core ist part from behind the scripts to the 'd'
|
2016-06-02 20:58:52 +00:00
|
|
|
auto p = make_unique<InsetMathExInt>(buf, from_ascii("int"));
|
2002-07-30 17:51:19 +00:00
|
|
|
|
|
|
|
// handle scripts if available
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!testIntSymbol(*it)) {
|
|
|
|
p->cell(2) = (*it)->asScriptInset()->down();
|
|
|
|
p->cell(3) = (*it)->asScriptInset()->up();
|
2002-07-30 17:51:19 +00:00
|
|
|
}
|
2009-11-08 11:45:46 +00:00
|
|
|
p->cell(0) = MathData(buf, it + 1, jt);
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// use the "thing" behind the 'd' as differential
|
2009-11-14 20:20:17 +00:00
|
|
|
MathData::iterator tt = extractArgument(p->cell(1), jt + 1, ar.end(), kind);
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// remove used parts
|
|
|
|
ar.erase(it + 1, tt);
|
2003-11-03 17:47:28 +00:00
|
|
|
*it = MathAtom(p.release());
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nIntegrals to: " << ar << endl;
|
2001-11-09 14:23:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
bool testTermDelimiter(MathAtom const & at)
|
|
|
|
{
|
|
|
|
return testString(at, "+") || testString(at, "-");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// try to extract a "term", i.e., something delimited by '+' or '-'.
|
|
|
|
// returns position behind the term
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator extractTerm(MathData & ar,
|
|
|
|
MathData::iterator pos, MathData::iterator last)
|
2006-09-02 01:57:36 +00:00
|
|
|
{
|
|
|
|
while (pos != last && !testTermDelimiter(*pos)) {
|
|
|
|
ar.push_back(*pos);
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-12 14:37:43 +00:00
|
|
|
//
|
|
|
|
// search sums
|
|
|
|
//
|
|
|
|
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
bool testEqualSign(MathAtom const & at)
|
2001-11-12 14:37:43 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testString(at, "=");
|
2001-11-12 14:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-17 17:42:21 +00:00
|
|
|
bool testSumSymbol(MathAtom const & p)
|
2002-07-30 17:51:19 +00:00
|
|
|
{
|
2009-12-17 17:42:21 +00:00
|
|
|
return testSymbol(p, from_ascii("sum"));
|
2002-07-30 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-17 17:42:21 +00:00
|
|
|
bool testSum(MathAtom const & at)
|
2002-07-30 17:51:19 +00:00
|
|
|
{
|
2009-12-17 17:42:21 +00:00
|
|
|
return
|
|
|
|
testSumSymbol(at) ||
|
|
|
|
( at->asScriptInset()
|
2012-10-21 19:14:16 +00:00
|
|
|
&& !at->asScriptInset()->nuc().empty()
|
2009-12-17 17:42:21 +00:00
|
|
|
&& testSumSymbol(at->asScriptInset()->nuc().back()) );
|
2002-07-30 17:51:19 +00:00
|
|
|
}
|
|
|
|
|
2001-11-12 14:37:43 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
// replace '\sum' ['_^'] f(x) sequences by a real InsetMathExInt
|
2001-11-12 14:37:43 +00:00
|
|
|
// assume 'extractDelims' ran before
|
2009-12-17 17:42:21 +00:00
|
|
|
void extractSums(MathData & ar)
|
2001-11-12 14:37:43 +00:00
|
|
|
{
|
|
|
|
// we need at least two items...
|
2002-07-12 11:21:21 +00:00
|
|
|
if (ar.size() < 2)
|
2001-11-12 14:37:43 +00:00
|
|
|
return;
|
|
|
|
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
|
|
|
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nSums from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i + 1 < ar.size(); ++i) {
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator it = ar.begin() + i;
|
2001-11-12 14:37:43 +00:00
|
|
|
|
|
|
|
// is this a sum name?
|
2009-12-17 17:42:21 +00:00
|
|
|
if (!testSum(ar[i]))
|
2001-11-12 14:37:43 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
// create a proper inset as replacement
|
2016-06-02 20:58:52 +00:00
|
|
|
auto p = make_unique<InsetMathExInt>(buf, from_ascii("sum"));
|
2001-11-12 14:37:43 +00:00
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// collect lower bound and summation index
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathScript const * sub = ar[i]->asScriptInset();
|
2002-07-30 17:51:19 +00:00
|
|
|
if (sub && sub->hasDown()) {
|
|
|
|
// try to figure out the summation index from the subscript
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData const & ar = sub->down();
|
|
|
|
MathData::const_iterator xt =
|
2002-07-30 17:51:19 +00:00
|
|
|
find_if(ar.begin(), ar.end(), &testEqualSign);
|
|
|
|
if (xt != ar.end()) {
|
|
|
|
// we found a '=', use everything in front of that as index,
|
|
|
|
// and everything behind as lower index
|
2009-11-08 11:45:46 +00:00
|
|
|
p->cell(1) = MathData(buf, ar.begin(), xt);
|
|
|
|
p->cell(2) = MathData(buf, xt + 1, ar.end());
|
2002-07-30 17:51:19 +00:00
|
|
|
} else {
|
|
|
|
// use everything as summation index, don't use scripts.
|
|
|
|
p->cell(1) = ar;
|
|
|
|
}
|
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// collect upper bound
|
2002-07-30 17:51:19 +00:00
|
|
|
if (sub && sub->hasUp())
|
2002-08-02 14:29:42 +00:00
|
|
|
p->cell(3) = sub->up();
|
2001-11-12 14:37:43 +00:00
|
|
|
|
2002-07-30 17:51:19 +00:00
|
|
|
// use something behind the script as core
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator tt = extractTerm(p->cell(0), it + 1, ar.end());
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
ar.erase(it + 1, tt);
|
2003-11-03 17:47:28 +00:00
|
|
|
*it = MathAtom(p.release());
|
2001-11-12 14:37:43 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nSums to: " << ar << endl;
|
2001-11-12 14:37:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-11-13 16:27:06 +00:00
|
|
|
//
|
|
|
|
// search differential stuff
|
|
|
|
//
|
|
|
|
|
2001-11-13 18:33:48 +00:00
|
|
|
// tests for 'd' or '\partial'
|
2001-11-15 14:14:37 +00:00
|
|
|
bool testDiffItem(MathAtom const & at)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2006-09-02 01:57:36 +00:00
|
|
|
if (testString(at, "d") || testSymbol(at, "partial"))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// we may have d^n .../d and splitScripts() has not yet seen it
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathScript const * sup = at->asScriptInset();
|
2006-09-02 01:57:36 +00:00
|
|
|
if (sup && !sup->hasDown() && sup->hasUp() && sup->nuc().size() == 1) {
|
|
|
|
MathAtom const & ma = sup->nuc().front();
|
|
|
|
return testString(ma, "d") || testSymbol(ma, "partial");
|
|
|
|
}
|
|
|
|
return false;
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
bool testDiffArray(MathData const & ar)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2012-10-21 19:14:16 +00:00
|
|
|
return !ar.empty() && testDiffItem(ar.front());
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-08 16:44:06 +00:00
|
|
|
bool testDiffFrac(MathAtom const & at)
|
2001-11-13 18:33:48 +00:00
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return
|
|
|
|
at->asFracInset()
|
|
|
|
&& testDiffArray(at->asFracInset()->cell(0))
|
|
|
|
&& testDiffArray(at->asFracInset()->cell(1));
|
2001-11-15 14:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractDiff(MathData & ar)
|
2001-11-13 16:27:06 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nDiffs from: " << ar << endl;
|
2004-01-30 11:41:12 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator it = ar.begin() + i;
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// is this a "differential fraction"?
|
2002-08-08 16:44:06 +00:00
|
|
|
if (!testDiffFrac(*it))
|
2001-11-13 18:33:48 +00:00
|
|
|
continue;
|
2002-03-21 17:42:56 +00:00
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathFrac const * f = (*it)->asFracInset();
|
2001-11-13 18:33:48 +00:00
|
|
|
if (!f) {
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "should not happen" << endl;
|
2001-11-13 18:33:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a proper diff inset
|
2016-06-02 20:58:52 +00:00
|
|
|
auto diff = make_unique<InsetMathDiff>(buf);
|
2001-11-13 18:33:48 +00:00
|
|
|
|
2001-11-15 09:51:57 +00:00
|
|
|
// collect function, let jt point behind last used item
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator jt = it + 1;
|
2002-03-21 17:42:56 +00:00
|
|
|
//int n = 1;
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData numer(f->cell(0));
|
2006-09-02 01:57:36 +00:00
|
|
|
splitScripts(numer);
|
2002-06-18 15:44:30 +00:00
|
|
|
if (numer.size() > 1 && numer[1]->asScriptInset()) {
|
2001-11-15 09:51:57 +00:00
|
|
|
// this is something like d^n f(x) / d... or d^n / d...
|
2001-11-15 14:14:37 +00:00
|
|
|
// FIXME
|
2002-03-21 17:42:56 +00:00
|
|
|
//n = 1;
|
|
|
|
if (numer.size() > 2)
|
2009-11-08 11:45:46 +00:00
|
|
|
diff->cell(0) = MathData(buf, numer.begin() + 2, numer.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
else
|
2006-09-02 01:57:36 +00:00
|
|
|
jt = extractTerm(diff->cell(0), jt, ar.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
} else {
|
|
|
|
// simply d f(x) / d... or d/d...
|
2002-03-21 17:42:56 +00:00
|
|
|
if (numer.size() > 1)
|
2009-11-08 11:45:46 +00:00
|
|
|
diff->cell(0) = MathData(buf, numer.begin() + 1, numer.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
else
|
2006-09-02 01:57:36 +00:00
|
|
|
jt = extractTerm(diff->cell(0), jt, ar.end());
|
2001-11-15 09:51:57 +00:00
|
|
|
}
|
|
|
|
|
2001-11-15 14:14:37 +00:00
|
|
|
// collect denominator parts
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData denom(f->cell(1));
|
2006-09-02 01:57:36 +00:00
|
|
|
splitScripts(denom);
|
2007-04-26 16:05:57 +00:00
|
|
|
for (MathData::iterator dt = denom.begin(); dt != denom.end();) {
|
2001-11-15 14:14:37 +00:00
|
|
|
// find the next 'd'
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator et
|
2002-08-09 07:01:17 +00:00
|
|
|
= find_if(dt + 1, denom.end(), &testDiffItem);
|
2001-11-15 14:14:37 +00:00
|
|
|
|
|
|
|
// point before this
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator st = et - 1;
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathScript const * script = (*st)->asScriptInset();
|
2001-11-15 14:14:37 +00:00
|
|
|
if (script && script->hasUp()) {
|
|
|
|
// things like d.../dx^n
|
|
|
|
int mult = 1;
|
2002-08-02 14:29:42 +00:00
|
|
|
if (extractNumber(script->up(), mult)) {
|
2002-02-16 15:59:55 +00:00
|
|
|
//lyxerr << "mult: " << mult << endl;
|
2017-04-05 08:22:52 +00:00
|
|
|
if (mult < 0 || mult > 1000) {
|
|
|
|
lyxerr << "Cannot differentiate less than 0 or more than 1000 times !" << endl;
|
2017-03-28 09:17:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
for (int i = 0; i < mult; ++i)
|
2009-11-08 11:45:46 +00:00
|
|
|
diff->addDer(MathData(buf, dt + 1, st));
|
2001-11-15 14:14:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// just d.../dx
|
2009-11-08 11:45:46 +00:00
|
|
|
diff->addDer(MathData(buf, dt + 1, et));
|
2001-11-15 09:51:57 +00:00
|
|
|
}
|
2001-11-15 14:14:37 +00:00
|
|
|
dt = et;
|
2001-11-15 09:51:57 +00:00
|
|
|
}
|
2001-11-13 18:33:48 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
ar.erase(it + 1, jt);
|
2003-11-03 17:47:28 +00:00
|
|
|
*it = MathAtom(diff.release());
|
2001-11-13 18:33:48 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nDiffs to: " << ar << endl;
|
2001-11-13 16:27:06 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 08:26:41 +00:00
|
|
|
|
2002-07-12 11:21:21 +00:00
|
|
|
//
|
|
|
|
// search limits
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
bool testRightArrow(MathAtom const & at)
|
|
|
|
{
|
2002-08-08 16:44:06 +00:00
|
|
|
return testSymbol(at, "to") || testSymbol(at, "rightarrow");
|
2002-07-12 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-09-16 18:11:38 +00:00
|
|
|
// replace '\lim_{x->x0} f(x)' sequences by a real InsetMathLim
|
2002-07-12 11:21:21 +00:00
|
|
|
// assume 'extractDelims' ran before
|
2007-04-26 16:05:57 +00:00
|
|
|
void extractLims(MathData & ar)
|
2002-07-12 11:21:21 +00:00
|
|
|
{
|
2009-11-08 11:45:46 +00:00
|
|
|
Buffer * buf = ar.buffer();
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nLimits from: " << ar << endl;
|
2006-09-02 01:57:36 +00:00
|
|
|
for (size_t i = 0; i < ar.size(); ++i) {
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData::iterator it = ar.begin() + i;
|
2002-07-12 11:21:21 +00:00
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// must be a script inset with a subscript (without superscript)
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathScript const * sub = (*it)->asScriptInset();
|
2006-09-02 01:57:36 +00:00
|
|
|
if (!sub || !sub->hasDown() || sub->hasUp() || sub->nuc().size() != 1)
|
2002-07-12 11:21:21 +00:00
|
|
|
continue;
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// is this a limit function?
|
|
|
|
if (!testSymbol(sub->nuc().front(), "lim"))
|
2002-07-12 11:21:21 +00:00
|
|
|
continue;
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
// subscript must contain a -> symbol
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData const & s = sub->down();
|
|
|
|
MathData::const_iterator st = find_if(s.begin(), s.end(), &testRightArrow);
|
2002-07-12 11:21:21 +00:00
|
|
|
if (st == s.end())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// the -> splits the subscript int x and x0
|
2009-11-08 11:45:46 +00:00
|
|
|
MathData x = MathData(buf, s.begin(), st);
|
|
|
|
MathData x0 = MathData(buf, st + 1, s.end());
|
2002-11-04 02:12:42 +00:00
|
|
|
|
2002-07-12 11:21:21 +00:00
|
|
|
// use something behind the script as core
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData f;
|
|
|
|
MathData::iterator tt = extractTerm(f, it + 1, ar.end());
|
2002-07-12 11:21:21 +00:00
|
|
|
|
|
|
|
// cleanup
|
|
|
|
ar.erase(it + 1, tt);
|
2002-08-08 16:08:11 +00:00
|
|
|
|
|
|
|
// create a proper inset as replacement
|
2009-11-08 11:45:46 +00:00
|
|
|
*it = MathAtom(new InsetMathLim(buf, f, x, x0));
|
2002-07-12 11:21:21 +00:00
|
|
|
}
|
2003-08-02 11:30:30 +00:00
|
|
|
//lyxerr << "\nLimits to: " << ar << endl;
|
2002-07-12 11:21:21 +00:00
|
|
|
}
|
|
|
|
|
2001-11-16 08:26:41 +00:00
|
|
|
|
2001-11-13 16:27:06 +00:00
|
|
|
//
|
|
|
|
// combine searches
|
|
|
|
//
|
|
|
|
|
2009-11-14 20:20:17 +00:00
|
|
|
void extractStructure(MathData & ar, ExternalMath kind)
|
2001-11-09 10:44:24 +00:00
|
|
|
{
|
2011-04-01 19:28:26 +00:00
|
|
|
//lyxerr << "\nStructure from: " << ar << endl;
|
2010-03-29 22:52:13 +00:00
|
|
|
if (kind != MATHML && kind != HTML)
|
2009-12-17 17:52:09 +00:00
|
|
|
splitScripts(ar);
|
2006-09-02 01:57:36 +00:00
|
|
|
extractDelims(ar);
|
2009-11-14 20:20:17 +00:00
|
|
|
extractIntegrals(ar, kind);
|
2010-03-29 22:52:13 +00:00
|
|
|
if (kind != MATHML && kind != HTML)
|
2009-12-17 17:52:09 +00:00
|
|
|
extractSums(ar);
|
2002-06-18 15:44:30 +00:00
|
|
|
extractNumbers(ar);
|
2001-11-09 10:44:24 +00:00
|
|
|
extractMatrices(ar);
|
2010-03-29 22:52:13 +00:00
|
|
|
if (kind != MATHML && kind != HTML) {
|
2013-03-27 16:05:00 +00:00
|
|
|
extractFunctions(ar, kind);
|
2009-12-31 21:09:28 +00:00
|
|
|
extractDets(ar);
|
|
|
|
extractDiff(ar);
|
2010-01-21 21:05:53 +00:00
|
|
|
extractExps(ar);
|
2010-01-21 21:01:51 +00:00
|
|
|
extractLims(ar);
|
2009-11-14 20:20:17 +00:00
|
|
|
extractStrings(ar);
|
2010-01-21 21:01:51 +00:00
|
|
|
}
|
2011-04-01 19:28:26 +00:00
|
|
|
//lyxerr << "\nStructure to: " << ar << endl;
|
2001-11-09 10:44:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
namespace {
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
string captureOutput(string const & cmd, string const & data)
|
2002-06-18 15:44:30 +00:00
|
|
|
{
|
2007-01-16 18:10:10 +00:00
|
|
|
// In order to avoid parsing problems with command interpreters
|
|
|
|
// we pass input data through a file
|
2016-07-02 16:58:30 +00:00
|
|
|
// Since the CAS is supposed to read the temp file we need
|
|
|
|
// to unlock it on windows (bug 10262).
|
|
|
|
unique_ptr<TempFile> tempfile(new TempFile("casinput"));
|
|
|
|
tempfile->setAutoRemove(false);
|
|
|
|
FileName const cas_tmpfile = tempfile->name();
|
|
|
|
tempfile.reset();
|
|
|
|
|
2007-01-16 18:10:10 +00:00
|
|
|
if (cas_tmpfile.empty()) {
|
|
|
|
lyxerr << "Warning: cannot create temporary file."
|
|
|
|
<< endl;
|
2007-12-12 19:28:07 +00:00
|
|
|
return string();
|
2007-01-16 18:10:10 +00:00
|
|
|
}
|
2007-12-12 19:28:07 +00:00
|
|
|
ofstream os(cas_tmpfile.toFilesystemEncoding().c_str());
|
2007-01-16 18:10:10 +00:00
|
|
|
os << data << endl;
|
|
|
|
os.close();
|
2007-12-12 19:28:07 +00:00
|
|
|
string command = cmd + " < "
|
2007-01-16 18:10:10 +00:00
|
|
|
+ quoteName(cas_tmpfile.toFilesystemEncoding());
|
|
|
|
lyxerr << "calling: " << cmd
|
|
|
|
<< "\ninput: '" << data << "'" << endl;
|
2006-04-08 22:31:11 +00:00
|
|
|
cmd_ret const ret = runCommand(command);
|
2016-07-03 17:41:04 +00:00
|
|
|
cas_tmpfile.removeFile();
|
2002-10-28 17:15:19 +00:00
|
|
|
return ret.second;
|
|
|
|
}
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
size_t get_matching_brace(string const & str, size_t i)
|
2002-10-28 21:24:06 +00:00
|
|
|
{
|
|
|
|
int count = 1;
|
2006-10-22 10:15:23 +00:00
|
|
|
size_t n = str.size();
|
2002-10-28 21:24:06 +00:00
|
|
|
while (i < n) {
|
|
|
|
i = str.find_first_of("{}", i+1);
|
2006-10-22 10:15:23 +00:00
|
|
|
if (i == npos)
|
|
|
|
return i;
|
2002-10-28 21:24:06 +00:00
|
|
|
if (str[i] == '{')
|
|
|
|
++count;
|
|
|
|
else
|
|
|
|
--count;
|
|
|
|
if (count == 0)
|
|
|
|
return i;
|
|
|
|
}
|
2006-10-22 10:15:23 +00:00
|
|
|
return npos;
|
2002-10-28 21:24:06 +00:00
|
|
|
}
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
size_t get_matching_brace_back(string const & str, size_t i)
|
2002-10-28 21:24:06 +00:00
|
|
|
{
|
|
|
|
int count = 1;
|
|
|
|
while (i > 0) {
|
|
|
|
i = str.find_last_of("{}", i-1);
|
2006-10-22 10:15:23 +00:00
|
|
|
if (i == npos)
|
|
|
|
return i;
|
2002-10-28 21:24:06 +00:00
|
|
|
if (str[i] == '}')
|
|
|
|
++count;
|
|
|
|
else
|
|
|
|
--count;
|
|
|
|
if (count == 0)
|
|
|
|
return i;
|
|
|
|
}
|
2006-10-22 10:15:23 +00:00
|
|
|
return npos;
|
2002-10-28 21:24:06 +00:00
|
|
|
}
|
2002-10-28 17:15:19 +00:00
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData pipeThroughMaxima(docstring const &, MathData const & ar)
|
2002-10-28 17:15:19 +00:00
|
|
|
{
|
2006-10-21 00:16:43 +00:00
|
|
|
odocstringstream os;
|
2002-10-28 17:15:19 +00:00
|
|
|
MaximaStream ms(os);
|
|
|
|
ms << ar;
|
2006-10-22 10:15:23 +00:00
|
|
|
docstring expr = os.str();
|
|
|
|
docstring const header = from_ascii("simpsum:true;");
|
2002-10-28 17:15:19 +00:00
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
string out;
|
2002-10-28 17:15:19 +00:00
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
// try to fix missing '*' the hard way
|
|
|
|
//
|
|
|
|
// > echo "2x;" | maxima
|
|
|
|
// ...
|
|
|
|
// (C1) Incorrect syntax: x is not an infix operator
|
|
|
|
// 2x;
|
|
|
|
// ^
|
|
|
|
//
|
2006-10-22 10:15:23 +00:00
|
|
|
lyxerr << "checking expr: '" << to_utf8(expr) << "'" << endl;
|
|
|
|
docstring full = header + "tex(" + expr + ");";
|
|
|
|
out = captureOutput("maxima", to_utf8(full));
|
2002-10-28 17:15:19 +00:00
|
|
|
|
|
|
|
// leave loop if expression syntax is probably ok
|
2006-10-22 10:15:23 +00:00
|
|
|
if (out.find("Incorrect syntax") == npos)
|
2002-10-28 17:15:19 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
// search line with "Incorrect syntax"
|
2004-09-16 20:37:34 +00:00
|
|
|
istringstream is(out);
|
2007-12-12 19:28:07 +00:00
|
|
|
string line;
|
2002-10-28 17:15:19 +00:00
|
|
|
while (is) {
|
|
|
|
getline(is, line);
|
2006-10-22 10:15:23 +00:00
|
|
|
if (line.find("Incorrect syntax") != npos)
|
2002-10-28 17:15:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2nd next line is the one with caret
|
|
|
|
getline(is, line);
|
|
|
|
getline(is, line);
|
2006-10-22 10:15:23 +00:00
|
|
|
size_t pos = line.find('^');
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "found caret at pos: '" << pos << "'" << endl;
|
2006-10-22 10:15:23 +00:00
|
|
|
if (pos == npos || pos < 4)
|
2002-10-28 17:15:19 +00:00
|
|
|
break; // caret position not found
|
|
|
|
pos -= 4; // skip the "tex(" part
|
|
|
|
if (expr[pos] == '*')
|
|
|
|
break; // two '*' in a row are definitely bad
|
2006-10-22 10:15:23 +00:00
|
|
|
expr.insert(pos, from_ascii("*"));
|
2002-10-28 17:15:19 +00:00
|
|
|
}
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
vector<string> tmp = getVectorFromString(out, "$$");
|
2002-10-28 17:15:19 +00:00
|
|
|
if (tmp.size() < 2)
|
2007-04-26 16:05:57 +00:00
|
|
|
return MathData();
|
2002-10-28 17:15:19 +00:00
|
|
|
|
2016-06-26 11:22:44 +00:00
|
|
|
out = subst(subst(tmp[1], "\\>", string()), "{\\it ", "\\mathit{");
|
2007-01-16 18:10:10 +00:00
|
|
|
lyxerr << "output: '" << out << "'" << endl;
|
2002-10-28 21:24:06 +00:00
|
|
|
|
|
|
|
// Ugly code that tries to make the result prettier
|
2006-10-22 10:15:23 +00:00
|
|
|
size_t i = out.find("\\mathchoice");
|
|
|
|
while (i != npos) {
|
|
|
|
size_t j = get_matching_brace(out, i + 12);
|
|
|
|
size_t k = get_matching_brace(out, j + 1);
|
2002-10-28 21:24:06 +00:00
|
|
|
k = get_matching_brace(out, k + 1);
|
|
|
|
k = get_matching_brace(out, k + 1);
|
2007-12-12 19:28:07 +00:00
|
|
|
string mid = out.substr(i + 13, j - i - 13);
|
2006-10-22 10:15:23 +00:00
|
|
|
if (mid.find("\\over") != npos)
|
2002-10-28 21:24:06 +00:00
|
|
|
mid = '{' + mid + '}';
|
2008-09-08 01:18:33 +00:00
|
|
|
out = out.substr(0, i)
|
2002-10-28 21:24:06 +00:00
|
|
|
+ mid
|
|
|
|
+ out.substr(k + 1);
|
2007-01-16 18:10:10 +00:00
|
|
|
//lyxerr << "output: " << out << endl;
|
2002-10-28 21:24:06 +00:00
|
|
|
i = out.find("\\mathchoice", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
i = out.find("\\over");
|
2006-10-22 10:15:23 +00:00
|
|
|
while (i != npos) {
|
|
|
|
size_t j = get_matching_brace_back(out, i - 1);
|
|
|
|
if (j == npos || j == 0)
|
|
|
|
break;
|
|
|
|
size_t k = get_matching_brace(out, i + 5);
|
|
|
|
if (k == npos || k + 1 == out.size())
|
|
|
|
break;
|
2008-09-08 01:18:33 +00:00
|
|
|
out = out.substr(0, j - 1)
|
2002-10-28 21:24:06 +00:00
|
|
|
+ "\\frac"
|
2008-09-08 01:18:33 +00:00
|
|
|
+ out.substr(j, i - j)
|
|
|
|
+ out.substr(i + 5, k - i - 4)
|
2002-10-28 21:24:06 +00:00
|
|
|
+ out.substr(k + 2);
|
2007-01-16 18:10:10 +00:00
|
|
|
//lyxerr << "output: " << out << endl;
|
2002-10-28 21:24:06 +00:00
|
|
|
i = out.find("\\over", i + 4);
|
|
|
|
}
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData res;
|
2006-10-22 10:15:23 +00:00
|
|
|
mathed_parse_cell(res, from_utf8(out));
|
2002-10-28 17:15:19 +00:00
|
|
|
return res;
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData pipeThroughMaple(docstring const & extra, MathData const & ar)
|
2002-06-18 15:44:30 +00:00
|
|
|
{
|
2007-12-12 19:28:07 +00:00
|
|
|
string header = "readlib(latex):\n";
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
// remove the \\it for variable names
|
|
|
|
//"#`latex/csname_font` := `\\it `:"
|
|
|
|
header +=
|
|
|
|
"`latex/csname_font` := ``:\n";
|
|
|
|
|
|
|
|
// export matrices in (...) instead of [...]
|
|
|
|
header +=
|
|
|
|
"`latex/latex/matrix` := "
|
|
|
|
"subs(`[`=`(`, `]`=`)`,"
|
|
|
|
"eval(`latex/latex/matrix`)):\n";
|
|
|
|
|
|
|
|
// replace \\cdots with proper '*'
|
|
|
|
header +=
|
|
|
|
"`latex/latex/*` := "
|
|
|
|
"subs(`\\,`=`\\cdot `,"
|
|
|
|
"eval(`latex/latex/*`)):\n";
|
|
|
|
|
|
|
|
// remove spurious \\noalign{\\medskip} in matrix output
|
|
|
|
header +=
|
|
|
|
"`latex/latex/matrix`:= "
|
|
|
|
"subs(`\\\\\\\\\\\\noalign{\\\\medskip}` = `\\\\\\\\`,"
|
|
|
|
"eval(`latex/latex/matrix`)):\n";
|
|
|
|
|
|
|
|
//"#`latex/latex/symbol` "
|
|
|
|
// " := subs((\\'_\\' = \\'`\\_`\\',eval(`latex/latex/symbol`)): ";
|
|
|
|
|
2007-12-12 19:28:07 +00:00
|
|
|
string trailer = "quit;";
|
2006-10-21 00:16:43 +00:00
|
|
|
odocstringstream os;
|
2002-06-18 15:44:30 +00:00
|
|
|
MapleStream ms(os);
|
|
|
|
ms << ar;
|
2007-12-12 19:28:07 +00:00
|
|
|
string expr = to_utf8(os.str());
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "ar: '" << ar << "'\n"
|
2006-10-19 16:51:30 +00:00
|
|
|
<< "ms: '" << expr << "'" << endl;
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
// try to fix missing '*' the hard way by using mint
|
|
|
|
//
|
|
|
|
// ... > echo "1A;" | mint -i 1 -S -s -q
|
|
|
|
// on line 1: 1A;
|
|
|
|
// ^ syntax error -
|
|
|
|
// Probably missing an operator such as * p
|
|
|
|
//
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "checking expr: '" << expr << "'" << endl;
|
2002-11-27 10:30:28 +00:00
|
|
|
string out = captureOutput("mint -i 1 -S -s -q -q", expr + ';');
|
2002-06-18 15:44:30 +00:00
|
|
|
if (out.empty())
|
|
|
|
break; // expression syntax is ok
|
2004-09-16 20:37:34 +00:00
|
|
|
istringstream is(out);
|
2002-06-18 15:44:30 +00:00
|
|
|
string line;
|
|
|
|
getline(is, line);
|
2014-07-05 17:13:10 +00:00
|
|
|
if (!prefixIs(line, "on line"))
|
2002-06-18 15:44:30 +00:00
|
|
|
break; // error message not identified
|
|
|
|
getline(is, line);
|
2006-10-22 10:15:23 +00:00
|
|
|
size_t pos = line.find('^');
|
2002-06-18 15:44:30 +00:00
|
|
|
if (pos == string::npos || pos < 15)
|
|
|
|
break; // caret position not found
|
|
|
|
pos -= 15; // skip the "on line ..." part
|
|
|
|
if (expr[pos] == '*' || (pos > 0 && expr[pos - 1] == '*'))
|
|
|
|
break; // two '*' in a row are definitely bad
|
2002-11-27 10:30:28 +00:00
|
|
|
expr.insert(pos, 1, '*');
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
|
2006-10-19 16:51:30 +00:00
|
|
|
// FIXME UNICODE Is utf8 encoding correct?
|
2006-10-21 00:16:43 +00:00
|
|
|
string full = "latex(" + to_utf8(extra) + '(' + expr + "));";
|
2002-06-18 15:44:30 +00:00
|
|
|
string out = captureOutput("maple -q", header + full + trailer);
|
|
|
|
|
|
|
|
// change \_ into _
|
|
|
|
|
|
|
|
//
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData res;
|
2006-10-22 10:15:23 +00:00
|
|
|
mathed_parse_cell(res, from_utf8(out));
|
2002-06-18 15:44:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData pipeThroughOctave(docstring const &, MathData const & ar)
|
2002-06-18 15:44:30 +00:00
|
|
|
{
|
2006-10-21 00:16:43 +00:00
|
|
|
odocstringstream os;
|
2002-06-18 15:44:30 +00:00
|
|
|
OctaveStream vs(os);
|
|
|
|
vs << ar;
|
2006-10-21 00:16:43 +00:00
|
|
|
string expr = to_utf8(os.str());
|
2002-06-18 15:44:30 +00:00
|
|
|
string out;
|
2009-11-08 11:45:46 +00:00
|
|
|
// FIXME const cast
|
|
|
|
Buffer * buf = const_cast<Buffer *>(ar.buffer());
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "pipe: ar: '" << ar << "'\n"
|
|
|
|
<< "pipe: expr: '" << expr << "'" << endl;
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) { // at most 100 attempts
|
|
|
|
//
|
|
|
|
// try to fix missing '*' the hard way
|
|
|
|
// parse error:
|
|
|
|
// >>> ([[1 2 3 ];[2 3 1 ];[3 1 2 ]])([[1 2 3 ];[2 3 1 ];[3 1 2 ]])
|
|
|
|
// ^
|
|
|
|
//
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "checking expr: '" << expr << "'" << endl;
|
2002-06-18 15:44:30 +00:00
|
|
|
out = captureOutput("octave -q 2>&1", expr);
|
2007-01-16 18:10:10 +00:00
|
|
|
lyxerr << "output: '" << out << "'" << endl;
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
// leave loop if expression syntax is probably ok
|
|
|
|
if (out.find("parse error:") == string::npos)
|
|
|
|
break;
|
|
|
|
|
|
|
|
// search line with single caret
|
2004-09-16 20:37:34 +00:00
|
|
|
istringstream is(out);
|
2002-06-18 15:44:30 +00:00
|
|
|
string line;
|
|
|
|
while (is) {
|
|
|
|
getline(is, line);
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "skipping line: '" << line << "'" << endl;
|
2002-06-18 15:44:30 +00:00
|
|
|
if (line.find(">>> ") != string::npos)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// found line with error, next line is the one with caret
|
|
|
|
getline(is, line);
|
2006-10-22 10:15:23 +00:00
|
|
|
size_t pos = line.find('^');
|
2003-08-02 11:30:30 +00:00
|
|
|
lyxerr << "caret line: '" << line << "'" << endl;
|
|
|
|
lyxerr << "found caret at pos: '" << pos << "'" << endl;
|
2002-06-18 15:44:30 +00:00
|
|
|
if (pos == string::npos || pos < 4)
|
|
|
|
break; // caret position not found
|
|
|
|
pos -= 4; // skip the ">>> " part
|
|
|
|
if (expr[pos] == '*')
|
|
|
|
break; // two '*' in a row are definitely bad
|
2002-11-27 10:30:28 +00:00
|
|
|
expr.insert(pos, 1, '*');
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
|
2007-01-07 03:28:53 +00:00
|
|
|
// remove 'ans = ' taking into account that there may be an
|
|
|
|
// ansi control sequence before, such as '\033[?1034hans = '
|
|
|
|
size_t i = out.find("ans = ");
|
|
|
|
if (i == string::npos)
|
2007-04-26 16:05:57 +00:00
|
|
|
return MathData();
|
2007-01-07 03:28:53 +00:00
|
|
|
out = out.substr(i + 6);
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
// parse output as matrix or single number
|
2009-11-08 11:45:46 +00:00
|
|
|
MathAtom at(new InsetMathArray(buf, from_ascii("array"), from_utf8(out)));
|
2006-09-16 18:11:38 +00:00
|
|
|
InsetMathArray const * mat = at->asArrayInset();
|
2009-11-08 11:45:46 +00:00
|
|
|
MathData res(buf);
|
2002-06-18 15:44:30 +00:00
|
|
|
if (mat->ncols() == 1 && mat->nrows() == 1)
|
2002-07-30 13:56:02 +00:00
|
|
|
res.append(mat->cell(0));
|
2002-06-18 15:44:30 +00:00
|
|
|
else {
|
2009-11-08 11:45:46 +00:00
|
|
|
res.push_back(MathAtom(
|
|
|
|
new InsetMathDelim(buf, from_ascii("("), from_ascii(")"))));
|
2002-08-09 07:01:17 +00:00
|
|
|
res.back().nucleus()->cell(0).push_back(at);
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
|
|
|
|
string fromMathematicaName(string const & name)
|
|
|
|
{
|
|
|
|
if (name == "Sin") return "sin";
|
|
|
|
if (name == "Sinh") return "sinh";
|
|
|
|
if (name == "ArcSin") return "arcsin";
|
|
|
|
if (name == "Cos") return "cos";
|
|
|
|
if (name == "Cosh") return "cosh";
|
|
|
|
if (name == "ArcCos") return "arccos";
|
|
|
|
if (name == "Tan") return "tan";
|
|
|
|
if (name == "Tanh") return "tanh";
|
|
|
|
if (name == "ArcTan") return "arctan";
|
|
|
|
if (name == "Cot") return "cot";
|
|
|
|
if (name == "Coth") return "coth";
|
|
|
|
if (name == "Csc") return "csc";
|
|
|
|
if (name == "Sec") return "sec";
|
|
|
|
if (name == "Exp") return "exp";
|
|
|
|
if (name == "Log") return "log";
|
|
|
|
if (name == "Arg" ) return "arg";
|
|
|
|
if (name == "Det" ) return "det";
|
|
|
|
if (name == "GCD" ) return "gcd";
|
|
|
|
if (name == "Max" ) return "max";
|
|
|
|
if (name == "Min" ) return "min";
|
|
|
|
if (name == "Erf" ) return "erf";
|
|
|
|
if (name == "Erfc" ) return "erfc";
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void prettifyMathematicaOutput(string & out, string const & macroName,
|
|
|
|
bool roman, bool translate)
|
|
|
|
{
|
|
|
|
string const macro = "\\" + macroName + "{";
|
2006-10-22 10:15:23 +00:00
|
|
|
size_t const len = macro.length();
|
|
|
|
size_t i = out.find(macro);
|
2006-09-02 01:57:36 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
while (i != npos) {
|
|
|
|
size_t const j = get_matching_brace(out, i + len);
|
2006-09-02 01:57:36 +00:00
|
|
|
string const name = out.substr(i + len, j - i - len);
|
|
|
|
out = out.substr(0, i)
|
|
|
|
+ (roman ? "\\mathrm{" : "")
|
|
|
|
+ (translate ? fromMathematicaName(name) : name)
|
|
|
|
+ out.substr(roman ? j : j + 1);
|
2007-01-16 18:10:10 +00:00
|
|
|
//lyxerr << "output: " << out << endl;
|
2006-09-02 01:57:36 +00:00
|
|
|
i = out.find(macro, i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData pipeThroughMathematica(docstring const &, MathData const & ar)
|
2006-09-02 01:57:36 +00:00
|
|
|
{
|
2006-10-21 00:16:43 +00:00
|
|
|
odocstringstream os;
|
2006-09-02 01:57:36 +00:00
|
|
|
MathematicaStream ms(os);
|
|
|
|
ms << ar;
|
2006-10-19 16:51:30 +00:00
|
|
|
// FIXME UNICODE Is utf8 encoding correct?
|
2006-10-21 00:16:43 +00:00
|
|
|
string const expr = to_utf8(os.str());
|
2006-09-02 01:57:36 +00:00
|
|
|
string out;
|
|
|
|
|
|
|
|
lyxerr << "expr: '" << expr << "'" << endl;
|
|
|
|
|
|
|
|
string const full = "TeXForm[" + expr + "]";
|
|
|
|
out = captureOutput("math", full);
|
2007-01-16 18:10:10 +00:00
|
|
|
lyxerr << "output: '" << out << "'" << endl;
|
2006-09-02 01:57:36 +00:00
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
size_t pos1 = out.find("Out[1]//TeXForm= ");
|
|
|
|
size_t pos2 = out.find("In[2]:=");
|
2006-09-02 01:57:36 +00:00
|
|
|
|
|
|
|
if (pos1 == string::npos || pos2 == string::npos)
|
2007-04-26 16:05:57 +00:00
|
|
|
return MathData();
|
2006-09-02 01:57:36 +00:00
|
|
|
|
|
|
|
// get everything from pos1+17 to pos2
|
|
|
|
out = out.substr(pos1 + 17, pos2 - pos1 - 17);
|
|
|
|
out = subst(subst(out, '\r', ' '), '\n', ' ');
|
|
|
|
|
|
|
|
// tries to make the result prettier
|
|
|
|
prettifyMathematicaOutput(out, "Mfunction", true, true);
|
|
|
|
prettifyMathematicaOutput(out, "Muserfunction", true, false);
|
|
|
|
prettifyMathematicaOutput(out, "Mvariable", false, false);
|
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData res;
|
2006-10-22 10:15:23 +00:00
|
|
|
mathed_parse_cell(res, from_utf8(out));
|
2006-09-02 01:57:36 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
|
2009-11-14 20:20:17 +00:00
|
|
|
} // anon namespace
|
|
|
|
|
|
|
|
void write(MathData const & dat, WriteStream & wi)
|
|
|
|
{
|
|
|
|
wi.firstitem() = true;
|
2015-10-12 22:31:15 +00:00
|
|
|
docstring s;
|
|
|
|
for (MathData::const_iterator it = dat.begin(); it != dat.end(); ++it) {
|
|
|
|
InsetMathChar const * const c = (*it)->asCharInset();
|
|
|
|
if (c)
|
|
|
|
s += c->getChar();
|
|
|
|
else {
|
|
|
|
if (!s.empty()) {
|
|
|
|
writeString(s, wi);
|
|
|
|
s.clear();
|
|
|
|
}
|
|
|
|
(*it)->write(wi);
|
|
|
|
wi.firstitem() = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!s.empty()) {
|
|
|
|
writeString(s, wi);
|
2009-11-14 20:20:17 +00:00
|
|
|
wi.firstitem() = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-12 22:31:15 +00:00
|
|
|
void writeString(docstring const & s, WriteStream & os)
|
|
|
|
{
|
|
|
|
if (!os.latex() || os.lockedMode()) {
|
|
|
|
os << (os.asciiOnly() ? escape(s) : s);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
docstring::const_iterator cit = s.begin();
|
|
|
|
docstring::const_iterator end = s.end();
|
|
|
|
|
|
|
|
// We may already be inside an \ensuremath command.
|
|
|
|
bool in_forced_mode = os.pendingBrace();
|
|
|
|
|
|
|
|
// We will take care of matching braces.
|
|
|
|
os.pendingBrace(false);
|
|
|
|
|
|
|
|
while (cit != end) {
|
|
|
|
bool mathmode = in_forced_mode ? os.textMode() : !os.textMode();
|
|
|
|
char_type const c = *cit;
|
|
|
|
docstring command(1, c);
|
|
|
|
try {
|
|
|
|
bool termination = false;
|
|
|
|
if (isASCII(c) ||
|
|
|
|
Encodings::latexMathChar(c, mathmode, os.encoding(), command, termination)) {
|
|
|
|
if (os.textMode()) {
|
|
|
|
if (in_forced_mode) {
|
|
|
|
// we were inside \lyxmathsym
|
|
|
|
os << '}';
|
|
|
|
os.textMode(false);
|
|
|
|
in_forced_mode = false;
|
|
|
|
}
|
|
|
|
if (!isASCII(c) && os.textMode()) {
|
|
|
|
os << "\\ensuremath{";
|
|
|
|
os.textMode(false);
|
|
|
|
in_forced_mode = true;
|
|
|
|
}
|
|
|
|
} else if (isASCII(c) && in_forced_mode) {
|
|
|
|
// we were inside \ensuremath
|
|
|
|
os << '}';
|
|
|
|
os.textMode(true);
|
|
|
|
in_forced_mode = false;
|
|
|
|
}
|
|
|
|
} else if (!os.textMode()) {
|
|
|
|
if (in_forced_mode) {
|
|
|
|
// we were inside \ensuremath
|
|
|
|
os << '}';
|
|
|
|
in_forced_mode = false;
|
|
|
|
} else {
|
|
|
|
os << "\\lyxmathsym{";
|
|
|
|
in_forced_mode = true;
|
|
|
|
}
|
|
|
|
os.textMode(true);
|
|
|
|
}
|
|
|
|
os << command;
|
|
|
|
// We may need a space if the command contains a macro
|
|
|
|
// and the last char is ASCII.
|
|
|
|
if (termination)
|
|
|
|
os.pendingSpace(true);
|
|
|
|
} catch (EncodingException const & e) {
|
|
|
|
switch (os.output()) {
|
|
|
|
case WriteStream::wsDryrun: {
|
|
|
|
os << "<" << _("LyX Warning: ")
|
|
|
|
<< _("uncodable character") << " '";
|
|
|
|
os << docstring(1, e.failed_char);
|
|
|
|
os << "'>";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WriteStream::wsPreview: {
|
|
|
|
// indicate the encoding error by a boxed '?'
|
|
|
|
os << "{\\fboxsep=1pt\\fbox{?}}";
|
|
|
|
LYXERR0("Uncodable character" << " '"
|
|
|
|
<< docstring(1, e.failed_char)
|
|
|
|
<< "'");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case WriteStream::wsDefault:
|
|
|
|
default:
|
|
|
|
// throw again
|
|
|
|
throw(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++cit;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (in_forced_mode && os.textMode()) {
|
|
|
|
// We have to care for closing \lyxmathsym
|
|
|
|
os << '}';
|
|
|
|
os.textMode(false);
|
|
|
|
} else {
|
|
|
|
os.pendingBrace(in_forced_mode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-14 20:20:17 +00:00
|
|
|
void normalize(MathData const & ar, NormalStream & os)
|
|
|
|
{
|
|
|
|
for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->normalize(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void octave(MathData const & dat, OctaveStream & os)
|
|
|
|
{
|
|
|
|
MathData ar = dat;
|
|
|
|
extractStructure(ar, OCTAVE);
|
|
|
|
for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->octave(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void maple(MathData const & dat, MapleStream & os)
|
|
|
|
{
|
|
|
|
MathData ar = dat;
|
|
|
|
extractStructure(ar, MAPLE);
|
|
|
|
for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->maple(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void maxima(MathData const & dat, MaximaStream & os)
|
|
|
|
{
|
|
|
|
MathData ar = dat;
|
|
|
|
extractStructure(ar, MAXIMA);
|
|
|
|
for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->maxima(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void mathematica(MathData const & dat, MathematicaStream & os)
|
|
|
|
{
|
|
|
|
MathData ar = dat;
|
|
|
|
extractStructure(ar, MATHEMATICA);
|
|
|
|
for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->mathematica(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-31 15:46:39 +00:00
|
|
|
void mathmlize(MathData const & dat, MathStream & os)
|
2009-11-14 20:20:17 +00:00
|
|
|
{
|
|
|
|
MathData ar = dat;
|
|
|
|
extractStructure(ar, MATHML);
|
2012-10-21 19:14:16 +00:00
|
|
|
if (ar.empty())
|
2011-04-01 22:34:40 +00:00
|
|
|
os << "<mrow/>";
|
|
|
|
else if (ar.size() == 1)
|
2009-12-31 15:46:39 +00:00
|
|
|
os << ar.front();
|
2009-11-14 20:20:17 +00:00
|
|
|
else {
|
2011-04-01 22:34:40 +00:00
|
|
|
os << MTag("mrow");
|
2009-11-14 20:20:17 +00:00
|
|
|
for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
2009-12-31 15:46:39 +00:00
|
|
|
(*it)->mathmlize(os);
|
2011-04-01 22:34:40 +00:00
|
|
|
os << ETag("mrow");
|
2009-11-14 20:20:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-31 18:53:40 +00:00
|
|
|
|
2010-03-29 22:52:13 +00:00
|
|
|
void htmlize(MathData const & dat, HtmlStream & os)
|
|
|
|
{
|
|
|
|
MathData ar = dat;
|
|
|
|
extractStructure(ar, HTML);
|
2012-10-21 19:14:16 +00:00
|
|
|
if (ar.empty())
|
2010-03-29 22:52:13 +00:00
|
|
|
return;
|
|
|
|
if (ar.size() == 1) {
|
|
|
|
os << ar.front();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (MathData::const_iterator it = ar.begin(); it != ar.end(); ++it)
|
|
|
|
(*it)->htmlize(os);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-11-14 20:20:17 +00:00
|
|
|
// convert this inset somehow to a number
|
|
|
|
bool extractNumber(MathData const & ar, int & i)
|
|
|
|
{
|
|
|
|
idocstringstream is(charSequence(ar.begin(), ar.end()));
|
|
|
|
is >> i;
|
2014-03-04 22:04:27 +00:00
|
|
|
// Do not convert is implicitly to bool, since that is forbidden in C++11.
|
|
|
|
return !is.fail();
|
2009-11-14 20:20:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool extractNumber(MathData const & ar, double & d)
|
|
|
|
{
|
|
|
|
idocstringstream is(charSequence(ar.begin(), ar.end()));
|
|
|
|
is >> d;
|
2014-03-04 22:04:27 +00:00
|
|
|
// Do not convert is implicitly to bool, since that is forbidden in C++11.
|
|
|
|
return !is.fail();
|
2009-11-14 20:20:17 +00:00
|
|
|
}
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData pipeThroughExtern(string const & lang, docstring const & extra,
|
|
|
|
MathData const & ar)
|
2002-06-18 15:44:30 +00:00
|
|
|
{
|
|
|
|
if (lang == "octave")
|
|
|
|
return pipeThroughOctave(extra, ar);
|
|
|
|
|
2002-10-28 17:15:19 +00:00
|
|
|
if (lang == "maxima")
|
|
|
|
return pipeThroughMaxima(extra, ar);
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
if (lang == "maple")
|
|
|
|
return pipeThroughMaple(extra, ar);
|
|
|
|
|
2006-09-02 01:57:36 +00:00
|
|
|
if (lang == "mathematica")
|
|
|
|
return pipeThroughMathematica(extra, ar);
|
|
|
|
|
2002-06-18 15:44:30 +00:00
|
|
|
// create normalized expression
|
2006-10-21 00:16:43 +00:00
|
|
|
odocstringstream os;
|
2002-06-18 15:44:30 +00:00
|
|
|
NormalStream ns(os);
|
2002-11-27 10:30:28 +00:00
|
|
|
os << '[' << extra << ' ';
|
2002-06-18 15:44:30 +00:00
|
|
|
ns << ar;
|
2002-11-27 10:30:28 +00:00
|
|
|
os << ']';
|
2006-10-19 16:51:30 +00:00
|
|
|
// FIXME UNICODE Is utf8 encoding correct?
|
2006-10-21 00:16:43 +00:00
|
|
|
string data = to_utf8(os.str());
|
2002-06-18 15:44:30 +00:00
|
|
|
|
|
|
|
// search external script
|
2007-12-12 19:57:42 +00:00
|
|
|
FileName const file = libFileSearch("mathed", "extern_" + lang);
|
2002-06-18 15:44:30 +00:00
|
|
|
if (file.empty()) {
|
2002-11-27 10:30:28 +00:00
|
|
|
lyxerr << "converter to '" << lang << "' not found" << endl;
|
2007-04-26 16:05:57 +00:00
|
|
|
return MathData();
|
2002-06-18 15:44:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// run external sript
|
2010-04-21 01:19:09 +00:00
|
|
|
string out = captureOutput(file.absFileName(), data);
|
2007-04-26 16:05:57 +00:00
|
|
|
MathData res;
|
2006-10-22 10:15:23 +00:00
|
|
|
mathed_parse_cell(res, from_utf8(out));
|
2002-06-18 15:44:30 +00:00
|
|
|
return res;
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|