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 "support/std_sstream.h"
16 #include "support/filetools.h" // LibFileSearch
17 #include "math_data.h"
18 #include "math_inset.h"
19 #include "math_parser.h"
23 using lyx::support::LibFileSearch;
27 using std::istringstream;
39 bool correct(MathAtom & at, char c) const;
41 bool read(istream & is);
43 void write(ostream & os) const;
54 bool Correction::read(istream & 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(ostream & os) const
76 os << "from: '" << from1_ << "' and '" << from2_
77 << "' to '" << to_ << '\'' << endl;
81 bool Correction::correct(MathAtom & at, char c) const
83 //lyxerr[Debug::MATHED]
84 // << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
87 if (!at->match(from1_))
90 << "match found! subst in " << at
91 << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
97 istream & operator>>(istream & is, Correction & corr)
104 ostream & operator<<(ostream & os, Correction & corr)
116 typedef vector<Correction>::const_iterator const_iterator;
120 void insert(const Correction & corr) { data_.push_back(corr); }
122 bool correct(MathAtom & at, char c) const;
125 vector<Correction> data_;
129 bool Corrections::correct(MathAtom & at, char c) const
131 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
132 if (it->correct(at, c))
138 Corrections theCorrections;
140 void initAutoCorrect()
142 lyxerr[Debug::MATHED] << "reading autocorrect file" << endl;
143 string const file = LibFileSearch(string(), "autocorrect");
145 lyxerr << "Could not find autocorrect file" << endl;
150 ifstream is(file.c_str());
151 while (getline(is, line)) {
152 if (line.size() == 0 || line[0] == '#') {
153 //lyxerr[Debug::MATHED] << "ignoring line '" << line << '\'' << endl;
156 istringstream il(line);
158 //lyxerr[Debug::MATHED] << "line '" << line << '\'' << endl;
161 //lyxerr[Debug::MATHED] << "parsed: '" << corr << '\'' << endl;
162 theCorrections.insert(corr);
166 lyxerr[Debug::MATHED] << "done reading autocorrections." << endl;
173 bool math_autocorrect(MathAtom & at, char c)
175 static bool initialized = false;
182 return theCorrections.correct(at, c);