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.
15 #include "support/filetools.h" // LibFileSearch
16 #include "math_data.h"
17 #include "math_inset.h"
18 #include "math_parser.h"
22 using namespace lyx::support;
37 bool correct(MathAtom & at, char c) const;
39 bool read(istream & is);
41 void write(ostream & os) const;
52 bool Correction::read(istream & is)
61 mathed_parse_cell(ar1, s1);
62 mathed_parse_cell(ar3, s3);
63 if (ar1.size() != 1 || ar3.size() != 1)
72 void Correction::write(ostream & os) const
74 os << "from: '" << from1_ << "' and '" << from2_
75 << "' to '" << to_ << '\'' << endl;
79 bool Correction::correct(MathAtom & at, char c) const
81 //lyxerr[Debug::MATHED]
82 // << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
85 if (!at->match(from1_))
88 << "match found! subst in " << at
89 << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
95 istream & operator>>(istream & is, Correction & corr)
102 ostream & operator<<(ostream & os, Correction & corr)
114 typedef vector<Correction>::const_iterator const_iterator;
118 void insert(const Correction & corr) { data_.push_back(corr); }
120 bool correct(MathAtom & at, char c) const;
123 vector<Correction> data_;
127 bool Corrections::correct(MathAtom & at, char c) const
129 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
130 if (it->correct(at, c))
136 Corrections theCorrections;
138 void initAutoCorrect()
140 lyxerr[Debug::MATHED] << "reading autocorrect file" << endl;
141 string const file = LibFileSearch(string(), "autocorrect");
143 lyxerr << "Could not find autocorrect file" << endl;
148 ifstream is(file.c_str());
149 while (getline(is, line)) {
150 if (line.size() == 0 || line[0] == '#') {
151 //lyxerr[Debug::MATHED] << "ignoring line '" << line << '\'' << endl;
154 istringstream il(STRCONV(line));
156 //lyxerr[Debug::MATHED] << "line '" << line << '\'' << endl;
159 //lyxerr[Debug::MATHED] << "parsed: '" << corr << '\'' << endl;
160 theCorrections.insert(corr);
164 lyxerr[Debug::MATHED] << "done reading autocorrections." << endl;
171 bool math_autocorrect(MathAtom & at, char c)
173 static bool initialized = false;
180 return theCorrections.correct(at, c);