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.
|
|
|
|
*
|
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.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2008-02-18 07:14:42 +00:00
|
|
|
#include "support/debug.h"
|
2007-12-17 16:04:46 +00:00
|
|
|
#include "support/FileName.h"
|
2004-01-07 18:28:50 +00:00
|
|
|
#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
|
|
|
|
2007-12-12 10:16:00 +00:00
|
|
|
using namespace std;
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
namespace lyx {
|
|
|
|
|
|
|
|
using support::libFileSearch;
|
2003-06-30 23:56:22 +00:00
|
|
|
|
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-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::MATHED,
|
|
|
|
// "trying to correct ar: " << at << " from: '" << from1_ << '\'');
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::MATHED, "match found! subst in " << at
|
|
|
|
<< " from: '" << from1_ << "' to '" << to_ << '\'');
|
2002-05-24 08:29:16 +00:00
|
|
|
at = to_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-02 16:27:32 +00:00
|
|
|
#if 0
|
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;
|
|
|
|
}
|
2013-05-02 16:27:32 +00:00
|
|
|
#endif
|
2002-05-24 08:29:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::MATHED, "reading autocorrect file");
|
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)) {
|
2012-10-21 19:14:16 +00:00
|
|
|
if (line.empty() || line[0] == '#') {
|
2007-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
|
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-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::MATHED, "line '" << line << '\'');
|
2002-05-24 08:29:16 +00:00
|
|
|
Correction corr;
|
|
|
|
if (corr.read(il)) {
|
2007-11-15 20:04:51 +00:00
|
|
|
//LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
|
2002-05-24 08:29:16 +00:00
|
|
|
theCorrections.insert(corr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-11-15 20:04:51 +00:00
|
|
|
LYXERR(Debug::MATHED, "done reading autocorrections.");
|
2002-05-24 08:29:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
} // namespace anon
|
|
|
|
|
|
|
|
|
2009-02-08 01:21:54 +00:00
|
|
|
bool math_autocorrect(MathAtom & at, char_type c)
|
2002-05-24 08:29:16 +00:00
|
|
|
{
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
initAutoCorrect();
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return theCorrections.correct(at, c);
|
|
|
|
}
|
2006-10-21 00:16:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
} // namespace lyx
|