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.
13 #include "MathAutoCorrect.h"
17 #include "InsetMath.h"
18 #include "MathSupport.h"
19 #include "MathParser.h"
21 #include "support/debug.h"
22 #include "support/FileName.h"
23 #include "support/filetools.h" // LibFileSearch
24 #include "support/docstream.h"
33 using support::libFileSearch;
41 Correction() : from2_(0) {}
43 bool correct(Cursor & cur, char_type c) const;
45 bool read(idocstream & is);
47 void write(odocstream & os) const;
58 bool Correction::read(idocstream & is)
67 mathed_parse_cell(ar1, s1);
68 mathed_parse_cell(ar3, s3);
76 bool Correction::correct(Cursor & cur, char_type c) const
78 //LYXERR(Debug::MATHED,
79 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
82 pos_type n = from1_.size();
83 if (cur.pos() < pos_type(from1_.size())) // not enough to match
85 pos_type start = cur.pos() - from1_.size();
87 for (pos_type i = 0; i < n; i++)
88 if (asString(cur.cell()[start + i]) != asString(from1_[i]))
91 LYXERR(Debug::MATHED, "match found! subst in " << cur.cell()
92 << " from: '" << from1_ << "' to '" << to_ << '\'');
94 /* To allow undoing the completion, we proceed in 4 steps
95 * - inset the raw character
96 * - split undo group so that we have two separate undo actions
97 * - record undo, delete the character we just entered and the from1_ part
98 * - finally, do the insertion of the correction.
101 cur.splitUndoGroup();
102 cur.recordUndoSelection();
103 cur.cell().erase(cur.pos() - n - 1, cur.pos());
112 void Correction::write(odocstream & os) const
114 os << "from: '" << from1_ << "' and '" << from2_
115 << "' to '" << to_ << '\'' << endl;
119 idocstream & operator>>(idocstream & is, Correction & corr)
126 odocstream & operator<<(odocstream & os, Correction & corr)
138 typedef vector<Correction>::const_iterator const_iterator;
142 void insert(const Correction & corr) { data_.push_back(corr); }
144 bool correct(Cursor & cur, char_type c) const;
147 vector<Correction> data_;
151 bool Corrections::correct(Cursor & cur, char_type c) const
153 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
154 if (it->correct(cur, c))
160 Corrections theCorrections;
162 void initAutoCorrect()
164 LYXERR(Debug::MATHED, "reading autocorrect file");
165 support::FileName const file = libFileSearch(string(), "autocorrect");
167 lyxerr << "Could not find autocorrect file" << endl;
172 ifstream is(file.toFilesystemEncoding().c_str());
173 while (getline(is, line)) {
174 if (line.empty() || line[0] == '#') {
175 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
178 idocstringstream il(from_utf8(line));
180 //LYXERR(Debug::MATHED, "line '" << line << '\'');
183 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
184 theCorrections.insert(corr);
188 LYXERR(Debug::MATHED, "done reading autocorrections.");
195 bool math_autocorrect(Cursor & cur, char_type c)
197 static bool initialized = false;
204 return theCorrections.correct(cur, c);