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"
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h" // LibFileSearch
22 #include "support/docstream.h"
31 using support::libFileSearch;
40 bool correct(MathAtom & at, char_type c) const;
42 bool read(idocstream & is);
44 void write(odocstream & os) const;
55 bool Correction::read(idocstream & is)
64 mathed_parse_cell(ar1, s1);
65 mathed_parse_cell(ar3, s3);
66 if (ar1.size() != 1 || ar3.size() != 1)
75 void Correction::write(odocstream & os) const
77 os << "from: '" << from1_ << "' and '" << from2_
78 << "' to '" << to_ << '\'' << endl;
82 bool Correction::correct(MathAtom & at, char_type c) const
84 //LYXERR(Debug::MATHED,
85 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
88 if (asString(at) != asString(from1_))
90 LYXERR(Debug::MATHED, "match found! subst in " << at
91 << " from: '" << from1_ << "' to '" << to_ << '\'');
98 idocstream & operator>>(idocstream & is, Correction & corr)
105 odocstream & operator<<(odocstream & os, Correction & corr)
117 typedef vector<Correction>::const_iterator const_iterator;
121 void insert(const Correction & corr) { data_.push_back(corr); }
123 bool correct(MathAtom & at, char_type c) const;
126 vector<Correction> data_;
130 bool Corrections::correct(MathAtom & at, char_type c) const
132 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
133 if (it->correct(at, c))
139 Corrections theCorrections;
141 void initAutoCorrect()
143 LYXERR(Debug::MATHED, "reading autocorrect file");
144 support::FileName const file = libFileSearch(string(), "autocorrect");
146 lyxerr << "Could not find autocorrect file" << endl;
151 ifstream is(file.toFilesystemEncoding().c_str());
152 while (getline(is, line)) {
153 if (line.empty() || line[0] == '#') {
154 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
157 idocstringstream il(from_utf8(line));
159 //LYXERR(Debug::MATHED, "line '" << line << '\'');
162 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
163 theCorrections.insert(corr);
167 LYXERR(Debug::MATHED, "done reading autocorrections.");
174 bool math_autocorrect(MathAtom & at, char_type c)
176 static bool initialized = false;
183 return theCorrections.correct(at, c);