2003-08-19 13:00:56 +00:00
|
|
|
|
/**
|
2007-04-25 03:01:35 +00:00
|
|
|
|
* \file MathAutoCorrect.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.
|
|
|
|
|
*
|
|
|
|
|
* \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>
|
|
|
|
|
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "MathAutoCorrect.h"
|
2007-04-26 16:05:57 +00:00
|
|
|
|
#include "MathData.h"
|
2006-09-17 09:14:18 +00:00
|
|
|
|
#include "InsetMath.h"
|
|
|
|
|
#include "MathSupport.h"
|
|
|
|
|
#include "MathParser.h"
|
2004-01-07 18:28:50 +00:00
|
|
|
|
#include "debug.h"
|
|
|
|
|
|
|
|
|
|
#include "support/filetools.h" // LibFileSearch
|
2007-11-05 22:54:53 +00:00
|
|
|
|
#include "support/docstream.h"
|
2002-05-24 08:29:16 +00:00
|
|
|
|
|
|
|
|
|
#include <fstream>
|
2004-07-24 10:55:30 +00:00
|
|
|
|
#include <sstream>
|
2002-05-24 08:29:16 +00:00
|
|
|
|
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
|
|
using support::libFileSearch;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
|
2003-10-06 15:43:21 +00:00
|
|
|
|
using std::string;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
using std::ifstream;
|
|
|
|
|
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() {}
|
|
|
|
|
///
|
2006-10-22 10:15:23 +00:00
|
|
|
|
bool correct(MathAtom & at, char_type c) const;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
///
|
2006-10-22 10:15:23 +00:00
|
|
|
|
bool read(idocstream & is);
|
2002-05-24 08:29:16 +00:00
|
|
|
|
///
|
2006-10-22 10:15:23 +00:00
|
|
|
|
void write(odocstream & os) const;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
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
|
|
|
|
///
|
2006-10-22 10:15:23 +00:00
|
|
|
|
char_type from2_;
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
2002-05-24 08:29:16 +00:00
|
|
|
|
MathAtom to_;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
bool Correction::read(idocstream & is)
|
2002-05-24 08:29:16 +00:00
|
|
|
|
{
|
2006-10-22 10:15:23 +00:00
|
|
|
|
docstring s1, s2, s3;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
is >> s1 >> s2 >> s3;
|
|
|
|
|
if (!is)
|
|
|
|
|
return false;
|
2002-06-18 15:44:30 +00:00
|
|
|
|
if (s2.size() != 1)
|
|
|
|
|
return false;
|
2007-04-26 16:05:57 +00:00
|
|
|
|
MathData ar1, ar3;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
void Correction::write(odocstream & os) const
|
2002-05-24 08:29:16 +00:00
|
|
|
|
{
|
|
|
|
|
os << "from: '" << from1_ << "' and '" << from2_
|
2002-11-27 10:30:28 +00:00
|
|
|
|
<< "' to '" << to_ << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
bool Correction::correct(MathAtom & at, char_type c) const
|
2002-05-24 08:29:16 +00:00
|
|
|
|
{
|
2007-04-01 10:09:49 +00:00
|
|
|
|
//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;
|
2004-01-07 18:28:50 +00:00
|
|
|
|
if (asString(at) != asString(from1_))
|
2002-05-24 08:29:16 +00:00
|
|
|
|
return false;
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::MATHED)
|
2002-05-24 08:29:16 +00:00
|
|
|
|
<< "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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
idocstream & operator>>(idocstream & is, Correction & corr)
|
2002-05-24 08:29:16 +00:00
|
|
|
|
{
|
|
|
|
|
corr.read(is);
|
|
|
|
|
return is;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
odocstream & operator<<(odocstream & os, Correction & corr)
|
2002-05-24 08:29:16 +00:00
|
|
|
|
{
|
|
|
|
|
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); }
|
|
|
|
|
///
|
2006-10-22 10:15:23 +00:00
|
|
|
|
bool correct(MathAtom & at, char_type c) const;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
private:
|
2002-05-24 10:01:07 +00:00
|
|
|
|
///
|
|
|
|
|
vector<Correction> data_;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2006-10-22 10:15:23 +00:00
|
|
|
|
bool Corrections::correct(MathAtom & at, char_type c) const
|
2002-05-24 08:29:16 +00:00
|
|
|
|
{
|
|
|
|
|
for (const_iterator it = data_.begin(); it != data_.end(); ++it)
|
|
|
|
|
if (it->correct(at, c))
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Corrections theCorrections;
|
|
|
|
|
|
|
|
|
|
void initAutoCorrect()
|
|
|
|
|
{
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::MATHED) << "reading autocorrect file" << endl;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
support::FileName const file = libFileSearch(string(), "autocorrect");
|
2002-05-24 08:29:16 +00:00
|
|
|
|
if (file.empty()) {
|
|
|
|
|
lyxerr << "Could not find autocorrect file" << endl;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
string line;
|
2006-11-26 21:30:39 +00:00
|
|
|
|
ifstream is(file.toFilesystemEncoding().c_str());
|
2002-05-24 08:29:16 +00:00
|
|
|
|
while (getline(is, line)) {
|
|
|
|
|
if (line.size() == 0 || line[0] == '#') {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
//LYXERR(Debug::MATHED) << "ignoring line '" << line << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2006-10-22 10:15:23 +00:00
|
|
|
|
idocstringstream il(from_utf8(line));
|
2002-11-04 02:12:42 +00:00
|
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
|
//LYXERR(Debug::MATHED) << "line '" << line << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
Correction corr;
|
|
|
|
|
if (corr.read(il)) {
|
2007-04-01 10:09:49 +00:00
|
|
|
|
//LYXERR(Debug::MATHED) << "parsed: '" << corr << '\'' << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
theCorrections.insert(corr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-01 10:09:49 +00:00
|
|
|
|
LYXERR(Debug::MATHED) << "done reading autocorrections." << endl;
|
2002-05-24 08:29:16 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool math_autocorrect(MathAtom & at, char c)
|
|
|
|
|
{
|
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
initAutoCorrect();
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return theCorrections.correct(at, c);
|
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|