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 /* To allow undoing the completion, we proceed in 4 steps
94 * - inset the raw character
95 * - split undo group so that we have two separate undo actions
96 * - record undo, delete the character we just entered and the from1_ part
97 * - finally, do the insertion of the correction.
100 cur.splitUndoGroup();
101 cur.recordUndoSelection();
102 cur.cell().erase(cur.pos() - n - 1, cur.pos());
111 void Correction::write(odocstream & os) const
113 os << "from: '" << from1_ << "' and '" << from2_
114 << "' to '" << to_ << '\'' << endl;
118 idocstream & operator>>(idocstream & is, Correction & corr)
125 odocstream & operator<<(odocstream & os, Correction & corr)
137 typedef vector<Correction>::const_iterator const_iterator;
141 void insert(const Correction & corr) { data_.push_back(corr); }
143 bool correct(Cursor & cur, char_type c) const;
146 vector<Correction> data_;
150 bool Corrections::correct(Cursor & cur, char_type c) const
152 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
153 if (it->correct(cur, c))
159 Corrections theCorrections;
161 void initAutoCorrect()
163 LYXERR(Debug::MATHED, "reading autocorrect file");
164 support::FileName const file = libFileSearch(string(), "autocorrect");
166 lyxerr << "Could not find autocorrect file" << endl;
171 ifstream is(file.toFilesystemEncoding().c_str());
172 while (getline(is, line)) {
173 if (line.empty() || line[0] == '#') {
174 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
177 idocstringstream il(from_utf8(line));
179 //LYXERR(Debug::MATHED, "line '" << line << '\'');
182 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
183 theCorrections.insert(corr);
187 LYXERR(Debug::MATHED, "done reading autocorrections.");
194 bool math_autocorrect(Cursor & cur, char_type c)
196 static bool initialized = false;
203 return theCorrections.correct(cur, c);