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;
39 Correction() : from2_(0) {}
41 bool correct(MathAtom & at, char_type c) const;
43 bool read(idocstream & is);
45 void write(odocstream & os) const;
56 bool Correction::read(idocstream & is)
65 mathed_parse_cell(ar1, s1);
66 mathed_parse_cell(ar3, s3);
67 if (ar1.size() != 1 || ar3.size() != 1)
76 bool Correction::correct(MathAtom & at, char_type c) const
78 //LYXERR(Debug::MATHED,
79 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
82 if (asString(at) != asString(from1_))
84 LYXERR(Debug::MATHED, "match found! subst in " << at
85 << " from: '" << from1_ << "' to '" << to_ << '\'');
92 void Correction::write(odocstream & os) const
94 os << "from: '" << from1_ << "' and '" << from2_
95 << "' to '" << to_ << '\'' << endl;
99 idocstream & operator>>(idocstream & is, Correction & corr)
106 odocstream & operator<<(odocstream & os, Correction & corr)
118 typedef vector<Correction>::const_iterator const_iterator;
122 void insert(const Correction & corr) { data_.push_back(corr); }
124 bool correct(MathAtom & at, char_type c) const;
127 vector<Correction> data_;
131 bool Corrections::correct(MathAtom & at, char_type c) const
133 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
134 if (it->correct(at, c))
140 Corrections theCorrections;
142 void initAutoCorrect()
144 LYXERR(Debug::MATHED, "reading autocorrect file");
145 support::FileName const file = libFileSearch(string(), "autocorrect");
147 lyxerr << "Could not find autocorrect file" << endl;
152 ifstream is(file.toFilesystemEncoding().c_str());
153 while (getline(is, line)) {
154 if (line.empty() || line[0] == '#') {
155 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
158 idocstringstream il(from_utf8(line));
160 //LYXERR(Debug::MATHED, "line '" << line << '\'');
163 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
164 theCorrections.insert(corr);
168 LYXERR(Debug::MATHED, "done reading autocorrections.");
175 bool math_autocorrect(MathAtom & at, char_type c)
177 static bool initialized = false;
184 return theCorrections.correct(at, c);