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
28 using support::libFileSearch;
42 bool correct(MathAtom & at, char_type c) const;
44 bool read(idocstream & is);
46 void write(odocstream & os) const;
57 bool Correction::read(idocstream & is)
66 mathed_parse_cell(ar1, s1);
67 mathed_parse_cell(ar3, s3);
68 if (ar1.size() != 1 || ar3.size() != 1)
77 void Correction::write(odocstream & os) const
79 os << "from: '" << from1_ << "' and '" << from2_
80 << "' to '" << to_ << '\'' << endl;
84 bool Correction::correct(MathAtom & at, char_type c) const
86 //LYXERR(Debug::MATHED)
87 // << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
90 if (asString(at) != asString(from1_))
93 << "match found! subst in " << at
94 << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
100 idocstream & operator>>(idocstream & is, Correction & corr)
107 odocstream & operator<<(odocstream & os, Correction & corr)
119 typedef vector<Correction>::const_iterator const_iterator;
123 void insert(const Correction & corr) { data_.push_back(corr); }
125 bool correct(MathAtom & at, char_type c) const;
128 vector<Correction> data_;
132 bool Corrections::correct(MathAtom & at, char_type c) const
134 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
135 if (it->correct(at, c))
141 Corrections theCorrections;
143 void initAutoCorrect()
145 LYXERR(Debug::MATHED) << "reading autocorrect file" << endl;
146 support::FileName const file = libFileSearch(string(), "autocorrect");
148 lyxerr << "Could not find autocorrect file" << endl;
153 ifstream is(file.toFilesystemEncoding().c_str());
154 while (getline(is, line)) {
155 if (line.size() == 0 || line[0] == '#') {
156 //LYXERR(Debug::MATHED) << "ignoring line '" << line << '\'' << endl;
159 idocstringstream il(from_utf8(line));
161 //LYXERR(Debug::MATHED) << "line '" << line << '\'' << endl;
164 //LYXERR(Debug::MATHED) << "parsed: '" << corr << '\'' << endl;
165 theCorrections.insert(corr);
169 LYXERR(Debug::MATHED) << "done reading autocorrections." << endl;
176 bool math_autocorrect(MathAtom & at, char c)
178 static bool initialized = false;
185 return theCorrections.correct(at, c);