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;
28 using std::istringstream;
40 bool correct(MathAtom & at, char c) const;
42 bool read(istream & is);
44 void write(ostream & os) const;
55 bool Correction::read(istream & is)
64 mathed_parse_cell(ar1, s1);
65 mathed_parse_cell(ar3, s3);
66 if (ar1.size() != 1 || ar3.size() != 1)
75 void Correction::write(ostream & os) const
77 os << "from: '" << from1_ << "' and '" << from2_
78 << "' to '" << to_ << '\'' << endl;
82 bool Correction::correct(MathAtom & at, char c) const
84 //lyxerr[Debug::MATHED]
85 // << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
88 if (!at->match(from1_))
91 << "match found! subst in " << at
92 << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
98 istream & operator>>(istream & is, Correction & corr)
105 ostream & operator<<(ostream & os, Correction & corr)
117 typedef vector<Correction>::const_iterator const_iterator;
121 void insert(const Correction & corr) { data_.push_back(corr); }
123 bool correct(MathAtom & at, char c) const;
126 vector<Correction> data_;
130 bool Corrections::correct(MathAtom & at, char c) const
132 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
133 if (it->correct(at, c))
139 Corrections theCorrections;
141 void initAutoCorrect()
143 lyxerr[Debug::MATHED] << "reading autocorrect file" << endl;
144 string const file = LibFileSearch(string(), "autocorrect");
146 lyxerr << "Could not find autocorrect file" << endl;
151 ifstream is(file.c_str());
152 while (getline(is, line)) {
153 if (line.size() == 0 || line[0] == '#') {
154 //lyxerr[Debug::MATHED] << "ignoring line '" << line << '\'' << endl;
157 istringstream il(line);
159 //lyxerr[Debug::MATHED] << "line '" << line << '\'' << endl;
162 //lyxerr[Debug::MATHED] << "parsed: '" << corr << '\'' << endl;
163 theCorrections.insert(corr);
167 lyxerr[Debug::MATHED] << "done reading autocorrections." << endl;
174 bool math_autocorrect(MathAtom & at, char c)
176 static bool initialized = false;
183 return theCorrections.correct(at, c);