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"
18 #include "support/debug.h"
20 #include "support/filetools.h" // LibFileSearch
21 #include "support/docstream.h"
30 using support::libFileSearch;
39 bool correct(MathAtom & at, char_type c) const;
41 bool read(idocstream & is);
43 void write(odocstream & os) const;
54 bool Correction::read(idocstream & is)
63 mathed_parse_cell(ar1, s1);
64 mathed_parse_cell(ar3, s3);
65 if (ar1.size() != 1 || ar3.size() != 1)
74 void Correction::write(odocstream & os) const
76 os << "from: '" << from1_ << "' and '" << from2_
77 << "' to '" << to_ << '\'' << endl;
81 bool Correction::correct(MathAtom & at, char_type c) const
83 //LYXERR(Debug::MATHED,
84 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
87 if (asString(at) != asString(from1_))
89 LYXERR(Debug::MATHED, "match found! subst in " << at
90 << " from: '" << from1_ << "' to '" << to_ << '\'');
96 idocstream & operator>>(idocstream & is, Correction & corr)
103 odocstream & operator<<(odocstream & os, Correction & corr)
115 typedef vector<Correction>::const_iterator const_iterator;
119 void insert(const Correction & corr) { data_.push_back(corr); }
121 bool correct(MathAtom & at, char_type c) const;
124 vector<Correction> data_;
128 bool Corrections::correct(MathAtom & at, char_type c) const
130 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
131 if (it->correct(at, c))
137 Corrections theCorrections;
139 void initAutoCorrect()
141 LYXERR(Debug::MATHED, "reading autocorrect file");
142 support::FileName const file = libFileSearch(string(), "autocorrect");
144 lyxerr << "Could not find autocorrect file" << endl;
149 ifstream is(file.toFilesystemEncoding().c_str());
150 while (getline(is, line)) {
151 if (line.size() == 0 || line[0] == '#') {
152 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
155 idocstringstream il(from_utf8(line));
157 //LYXERR(Debug::MATHED, "line '" << line << '\'');
160 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
161 theCorrections.insert(corr);
165 LYXERR(Debug::MATHED, "done reading autocorrections.");
172 bool math_autocorrect(MathAtom & at, char c)
174 static bool initialized = false;
181 return theCorrections.correct(at, c);