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 // it is fine not to initialize from2_
40 // coverity[UNINIT_CTOR]
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 bool Correction::correct(MathAtom & at, char_type c) const
80 //LYXERR(Debug::MATHED,
81 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
84 if (asString(at) != asString(from1_))
86 LYXERR(Debug::MATHED, "match found! subst in " << at
87 << " from: '" << from1_ << "' to '" << to_ << '\'');
94 void Correction::write(odocstream & os) const
96 os << "from: '" << from1_ << "' and '" << from2_
97 << "' 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");
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.empty() || line[0] == '#') {
157 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
160 idocstringstream il(from_utf8(line));
162 //LYXERR(Debug::MATHED, "line '" << line << '\'');
165 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
166 theCorrections.insert(corr);
170 LYXERR(Debug::MATHED, "done reading autocorrections.");
177 bool math_autocorrect(MathAtom & at, char_type c)
179 static bool initialized = false;
186 return theCorrections.correct(at, c);