2 * \file math_autocorrect.C
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 "math_autocorrect.h"
14 #include "math_data.h"
15 #include "math_inset.h"
16 #include "math_support.h"
17 #include "math_parser.h"
20 #include "support/filetools.h" // LibFileSearch
25 using lyx::support::LibFileSearch;
30 using std::istringstream;
42 bool correct(MathAtom & at, char c) const;
44 bool read(istream & is);
46 void write(ostream & os) const;
57 bool Correction::read(istream & 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(ostream & os) const
79 os << "from: '" << from1_ << "' and '" << from2_
80 << "' to '" << to_ << '\'' << endl;
84 bool Correction::correct(MathAtom & at, char 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 istream & operator>>(istream & is, Correction & corr)
107 ostream & operator<<(ostream & 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 c) const;
128 vector<Correction> data_;
132 bool Corrections::correct(MathAtom & at, char 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 string const file = LibFileSearch(string(), "autocorrect");
148 lyxerr << "Could not find autocorrect file" << endl;
153 ifstream is(file.c_str());
154 while (getline(is, line)) {
155 if (line.size() == 0 || line[0] == '#') {
156 //lyxerr[Debug::MATHED] << "ignoring line '" << line << '\'' << endl;
159 istringstream il(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);