2 * \file MathAutoCorrect.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "MathAutoCorrect.h"
15 #include "InsetMath.h"
16 #include "MathSupport.h"
17 #include "MathParser.h"
20 #include "support/filetools.h" // LibFileSearch
21 #include "support/docstream.h"
29 using support::libFileSearch;
43 bool correct(MathAtom & at, char_type c) const;
45 bool read(idocstream & is);
47 void write(odocstream & os) const;
58 bool Correction::read(idocstream & is)
67 mathed_parse_cell(ar1, s1);
68 mathed_parse_cell(ar3, s3);
69 if (ar1.size() != 1 || ar3.size() != 1)
78 void Correction::write(odocstream & os) const
80 os << "from: '" << from1_ << "' and '" << from2_
81 << "' to '" << to_ << '\'' << endl;
85 bool Correction::correct(MathAtom & at, char_type c) const
87 //LYXERR(Debug::MATHED)
88 // << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
91 if (asString(at) != asString(from1_))
94 << "match found! subst in " << at
95 << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
101 idocstream & operator>>(idocstream & is, Correction & corr)
108 odocstream & operator<<(odocstream & os, Correction & corr)
120 typedef vector<Correction>::const_iterator const_iterator;
124 void insert(const Correction & corr) { data_.push_back(corr); }
126 bool correct(MathAtom & at, char_type c) const;
129 vector<Correction> data_;
133 bool Corrections::correct(MathAtom & at, char_type c) const
135 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
136 if (it->correct(at, c))
142 Corrections theCorrections;
144 void initAutoCorrect()
146 LYXERR(Debug::MATHED) << "reading autocorrect file" << endl;
147 support::FileName const file = libFileSearch(string(), "autocorrect");
149 lyxerr << "Could not find autocorrect file" << endl;
154 ifstream is(file.toFilesystemEncoding().c_str());
155 while (getline(is, line)) {
156 if (line.size() == 0 || line[0] == '#') {
157 //LYXERR(Debug::MATHED) << "ignoring line '" << line << '\'' << endl;
160 idocstringstream il(from_utf8(line));
162 //LYXERR(Debug::MATHED) << "line '" << line << '\'' << endl;
165 //LYXERR(Debug::MATHED) << "parsed: '" << corr << '\'' << endl;
166 theCorrections.insert(corr);
170 LYXERR(Debug::MATHED) << "done reading autocorrections." << endl;
177 bool math_autocorrect(MathAtom & at, char c)
179 static bool initialized = false;
186 return theCorrections.correct(at, c);