A bit more re-organization.
[lyx.git] / src / mathed / MathAutoCorrect.cpp
blobfcc9f7705af445c692303fa50f579f59956790ce
1 /**
2 * \file MathAutoCorrect.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author André Pönitz
8 * Full author contact details are available in file CREDITS.
9 */
11 #include <config.h>
13 #include "MathAutoCorrect.h"
14 #include "MathData.h"
15 #include "InsetMath.h"
16 #include "MathSupport.h"
17 #include "MathParser.h"
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h" // LibFileSearch
22 #include "support/docstream.h"
24 #include <fstream>
25 #include <sstream>
27 using namespace std;
29 namespace lyx {
31 using support::libFileSearch;
33 namespace {
35 class Correction {
36 public:
37 ///
38 Correction() {}
39 ///
40 bool correct(MathAtom & at, char_type c) const;
41 ///
42 bool read(idocstream & is);
43 ///
44 void write(odocstream & os) const;
45 private:
46 ///
47 MathAtom from1_;
48 ///
49 char_type from2_;
50 ///
51 MathAtom to_;
55 bool Correction::read(idocstream & is)
57 docstring s1, s2, s3;
58 is >> s1 >> s2 >> s3;
59 if (!is)
60 return false;
61 if (s2.size() != 1)
62 return false;
63 MathData ar1, ar3;
64 mathed_parse_cell(ar1, s1);
65 mathed_parse_cell(ar3, s3);
66 if (ar1.size() != 1 || ar3.size() != 1)
67 return false;
68 from1_ = ar1.front();
69 from2_ = s2[0];
70 to_ = ar3.front();
71 return true;
75 void Correction::write(odocstream & os) const
77 os << "from: '" << from1_ << "' and '" << from2_
78 << "' to '" << to_ << '\'' << endl;
82 bool Correction::correct(MathAtom & at, char_type c) const
84 //LYXERR(Debug::MATHED,
85 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
86 if (from2_ != c)
87 return false;
88 if (asString(at) != asString(from1_))
89 return false;
90 LYXERR(Debug::MATHED, "match found! subst in " << at
91 << " from: '" << from1_ << "' to '" << to_ << '\'');
92 at = to_;
93 return true;
97 idocstream & operator>>(idocstream & is, Correction & corr)
99 corr.read(is);
100 return is;
104 odocstream & operator<<(odocstream & os, Correction & corr)
106 corr.write(os);
107 return os;
113 class Corrections {
114 public:
116 typedef vector<Correction>::const_iterator const_iterator;
118 Corrections() {}
120 void insert(const Correction & corr) { data_.push_back(corr); }
122 bool correct(MathAtom & at, char_type c) const;
123 private:
125 vector<Correction> data_;
129 bool Corrections::correct(MathAtom & at, char_type c) const
131 for (const_iterator it = data_.begin(); it != data_.end(); ++it)
132 if (it->correct(at, c))
133 return true;
134 return false;
138 Corrections theCorrections;
140 void initAutoCorrect()
142 LYXERR(Debug::MATHED, "reading autocorrect file");
143 support::FileName const file = libFileSearch(string(), "autocorrect");
144 if (file.empty()) {
145 lyxerr << "Could not find autocorrect file" << endl;
146 return;
149 string line;
150 ifstream is(file.toFilesystemEncoding().c_str());
151 while (getline(is, line)) {
152 if (line.size() == 0 || line[0] == '#') {
153 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
154 continue;
156 idocstringstream il(from_utf8(line));
158 //LYXERR(Debug::MATHED, "line '" << line << '\'');
159 Correction corr;
160 if (corr.read(il)) {
161 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
162 theCorrections.insert(corr);
166 LYXERR(Debug::MATHED, "done reading autocorrections.");
170 } // namespace anon
173 bool math_autocorrect(MathAtom & at, char_type c)
175 static bool initialized = false;
177 if (!initialized) {
178 initAutoCorrect();
179 initialized = true;
182 return theCorrections.correct(at, c);
186 } // namespace lyx