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 namespace lyx::support;
38 bool correct(MathAtom & at, char c) const;
40 bool read(istream & is);
42 void write(ostream & os) const;
53 bool Correction::read(istream & is)
62 mathed_parse_cell(ar1, s1);
63 mathed_parse_cell(ar3, s3);
64 if (ar1.size() != 1 || ar3.size() != 1)
73 void Correction::write(ostream & os) const
75 os << "from: '" << from1_ << "' and '" << from2_
76 << "' to '" << to_ << '\'' << endl;
80 bool Correction::correct(MathAtom & at, char c) const
82 //lyxerr[Debug::MATHED]
83 // << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
86 if (!at->match(from1_))
89 << "match found! subst in " << at
90 << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
96 istream & operator>>(istream & is, Correction & corr)
103 ostream & operator<<(ostream & 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 c) const;
124 vector<Correction> data_;
128 bool Corrections::correct(MathAtom & at, char 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" << endl;
142 string const file = LibFileSearch(string(), "autocorrect");
144 lyxerr << "Could not find autocorrect file" << endl;
149 ifstream is(file.c_str());
150 while (getline(is, line)) {
151 if (line.size() == 0 || line[0] == '#') {
152 //lyxerr[Debug::MATHED] << "ignoring line '" << line << '\'' << endl;
155 istringstream il(STRCONV(line));
157 //lyxerr[Debug::MATHED] << "line '" << line << '\'' << endl;
160 //lyxerr[Debug::MATHED] << "parsed: '" << corr << '\'' << endl;
161 theCorrections.insert(corr);
165 lyxerr[Debug::MATHED] << "done reading autocorrections." << endl;
172 bool math_autocorrect(MathAtom & at, char c)
174 static bool initialized = false;
181 return theCorrections.correct(at, c);