2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
|
|
|
|
* \file math_autocorrect.C
|
|
|
|
|
* This file is part of LyX, the document processor.
|
|
|
|
|
* Licence details can be found in the file COPYING.
|
|
|
|
|
*
|
|
|
|
|
* \author Andr<EFBFBD> P<EFBFBD>nitz
|
|
|
|
|
*
|
|
|
|
|
* Full author contact details are available in file CREDITS.
|
|
|
|
|
*/
|
|
|
|
|
|
2002-05-24 08:29:16 +00:00
|
|
|
|
#include <config.h>
|
|
|
|
|
|
2003-09-05 16:18:57 +00:00
|
|
|
|
#include "math_autocorrect.h"
|
2002-05-24 08:29:16 +00:00
|
|
|
|
#include "Lsstream.h"
|
|
|
|
|
#include "debug.h"
|
|
|
|
|
#include "support/filetools.h" // LibFileSearch
|
|
|
|
|
#include "math_data.h"
|
|
|
|
|
#include "math_inset.h"
|
|
|
|
|
#include "math_parser.h"
|
|
|
|
|
|
|
|
|
|
#include <fstream>
|
|
|
|
|
|
2003-06-30 23:56:22 +00:00
|
|
|
|
using namespace lyx::support;
|
|
|
|
|
|
2002-05-24 08:29:16 +00:00
|
|
|
|
using std::ifstream;
|
|
|
|
|
using std::istream;
|
|
|
|
|
using std::ostream;
|
|
|
|
|
using std::endl;
|
2002-05-24 10:01:07 +00:00
|
|
|
|
using std::vector;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class Correction {
|
|
|
|
|
public:
|
|
|
|
|
///
|
|
|
|
|
Correction() {}
|
|
|
|
|
///
|
|
|
|
|
bool correct(MathAtom & at, char c) const;
|
|
|
|
|
///
|
|
|
|
|
bool read(istream & is);
|
|
|
|
|
///
|
|
|
|
|
void write(ostream & os) const;
|
|
|
|
|
private:
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
2002-05-24 08:29:16 +00:00
|
|
|
|
MathAtom from1_;
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
2002-05-24 08:29:16 +00:00
|
|
|
|
char from2_;
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
2002-05-24 08:29:16 +00:00
|
|
|
|
MathAtom to_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Correction::read(istream & is)
|
|
|
|
|
{
|
|
|
|
|
string s1, s2, s3;
|
|
|
|
|
is >> s1 >> s2 >> s3;
|
|
|
|
|
if (!is)
|
|
|
|
|
return false;
|
2002-06-18 15:44:30 +00:00
|
|
|
|
if (s2.size() != 1)
|
|
|
|
|
return false;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
MathArray ar1, ar3;
|
|
|
|
|
mathed_parse_cell(ar1, s1);
|
|
|
|
|
mathed_parse_cell(ar3, s3);
|
2002-06-18 15:44:30 +00:00
|
|
|
|
if (ar1.size() != 1 || ar3.size() != 1)
|
2002-05-24 08:29:16 +00:00
|
|
|
|
return false;
|
|
|
|
|
from1_ = ar1.front();
|
|
|
|
|
from2_ = s2[0];
|
|
|
|
|
to_ = ar3.front();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Correction::write(ostream & os) const
|
|
|
|
|
{
|
|
|
|
|
os << "from: '" << from1_ << "' and '" << from2_
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< "' to '" << to_ << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Correction::correct(MathAtom & at, char c) const
|
|
|
|
|
{
|
|
|
|
|
//lyxerr[Debug::MATHED]
|
2002-11-27 10:30:28 +00:00
|
|
|
|
// << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
if (from2_ != c)
|
|
|
|
|
return false;
|
2002-08-09 10:22:35 +00:00
|
|
|
|
if (!at->match(from1_))
|
2002-05-24 08:29:16 +00:00
|
|
|
|
return false;
|
|
|
|
|
lyxerr[Debug::MATHED]
|
|
|
|
|
<< "match found! subst in " << at
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
at = to_;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
istream & operator>>(istream & is, Correction & corr)
|
|
|
|
|
{
|
|
|
|
|
corr.read(is);
|
|
|
|
|
return is;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ostream & operator<<(ostream & os, Correction & corr)
|
|
|
|
|
{
|
|
|
|
|
corr.write(os);
|
|
|
|
|
return os;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Corrections {
|
|
|
|
|
public:
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
2002-05-24 08:29:16 +00:00
|
|
|
|
typedef vector<Correction>::const_iterator const_iterator;
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
2002-05-24 08:29:16 +00:00
|
|
|
|
Corrections() {}
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
2002-05-24 08:29:16 +00:00
|
|
|
|
void insert(const Correction & corr) { data_.push_back(corr); }
|
|
|
|
|
///
|
|
|
|
|
bool correct(MathAtom & at, char c) const;
|
|
|
|
|
private:
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
|
|
|
|
vector<Correction> data_;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Corrections::correct(MathAtom & at, char c) const
|
|
|
|
|
{
|
|
|
|
|
for (const_iterator it = data_.begin(); it != data_.end(); ++it)
|
|
|
|
|
if (it->correct(at, c))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Corrections theCorrections;
|
|
|
|
|
|
|
|
|
|
void initAutoCorrect()
|
|
|
|
|
{
|
|
|
|
|
lyxerr[Debug::MATHED] << "reading autocorrect file" << endl;
|
|
|
|
|
string const file = LibFileSearch(string(), "autocorrect");
|
|
|
|
|
if (file.empty()) {
|
|
|
|
|
lyxerr << "Could not find autocorrect file" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string line;
|
|
|
|
|
ifstream is(file.c_str());
|
|
|
|
|
while (getline(is, line)) {
|
|
|
|
|
if (line.size() == 0 || line[0] == '#') {
|
2002-11-27 10:30:28 +00:00
|
|
|
|
//lyxerr[Debug::MATHED] << "ignoring line '" << line << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2002-11-04 02:12:42 +00:00
|
|
|
|
istringstream il(STRCONV(line));
|
|
|
|
|
|
2002-11-27 10:30:28 +00:00
|
|
|
|
//lyxerr[Debug::MATHED] << "line '" << line << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
Correction corr;
|
|
|
|
|
if (corr.read(il)) {
|
2002-11-27 10:30:28 +00:00
|
|
|
|
//lyxerr[Debug::MATHED] << "parsed: '" << corr << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
theCorrections.insert(corr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lyxerr[Debug::MATHED] << "done reading autocorrections." << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool math_autocorrect(MathAtom & at, char c)
|
|
|
|
|
{
|
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
initAutoCorrect();
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return theCorrections.correct(at, c);
|
|
|
|
|
}
|