2 * \file MathAutoCorrect.cpp
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.
14 #include "MathAutoCorrect.h"
16 #include "InsetMath.h"
17 #include "MathSupport.h"
18 #include "MathParser.h"
20 #include "support/debug.h"
21 #include "support/FileName.h"
22 #include "support/filetools.h" // LibFileSearch
23 #include "support/docstream.h"
32 using support::libFileSearch;
40 Correction() : from2_(0) {}
42 bool correct(Cursor & cur, char_type c) const;
44 bool read(idocstream & is);
46 void write(odocstream & os) const;
57 bool Correction::read(idocstream & is)
66 mathed_parse_cell(ar1, s1);
67 mathed_parse_cell(ar3, s3);
75 bool Correction::correct(Cursor & cur, char_type c) const
77 //LYXERR(Debug::MATHED,
78 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
81 pos_type n = from1_.size();
82 if (cur.pos() < pos_type(from1_.size())) // not enough to match
84 pos_type start = cur.pos() - from1_.size();
86 for (pos_type i = 0; i < n; i++)
87 if (asString(cur.cell()[start + i]) != asString(from1_[i]))
90 LYXERR(Debug::MATHED, "match found! subst in " << cur.cell()
91 << " from: '" << from1_ << "' to '" << to_ << '\'');
93 cur.cell().erase(cur.pos() - n, cur.pos());
102 void Correction::write(odocstream & os) const
104 os << "from: '" << from1_ << "' and '" << from2_
105 << "' to '" << to_ << '\'' << endl;
109 idocstream & operator>>(idocstream & is, Correction & corr)
116 odocstream & operator<<(odocstream & os, Correction & corr)
128 typedef vector<Correction>::const_iterator const_iterator;
132 void insert(const Correction & corr) { data_.push_back(corr); }
134 bool correct(Cursor & cur, char_type c) const;
137 vector<Correction> data_;
141 bool Corrections::correct(Cursor & cur, char_type c) const
143 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
144 if (it->correct(cur, c))
150 Corrections theCorrections;
152 void initAutoCorrect()
154 LYXERR(Debug::MATHED, "reading autocorrect file");
155 support::FileName const file = libFileSearch(string(), "autocorrect");
157 lyxerr << "Could not find autocorrect file" << endl;
162 ifstream is(file.toFilesystemEncoding().c_str());
163 while (getline(is, line)) {
164 if (line.empty() || line[0] == '#') {
165 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
168 idocstringstream il(from_utf8(line));
170 //LYXERR(Debug::MATHED, "line '" << line << '\'');
173 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
174 theCorrections.insert(corr);
178 LYXERR(Debug::MATHED, "done reading autocorrections.");
185 bool math_autocorrect(Cursor & cur, char_type c)
187 static bool initialized = false;
194 return theCorrections.correct(cur, c);