]> git.lyx.org Git - lyx.git/blob - src/mathed/MathAutoCorrect.cpp
Revert some recent test changes
[lyx.git] / src / mathed / MathAutoCorrect.cpp
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.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathAutoCorrect.h"
14 #include "MathData.h"
15 #include "InsetMath.h"
16 #include "MathSupport.h"
17 #include "MathParser.h"
18
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h" //  LibFileSearch
22 #include "support/docstream.h"
23
24 #include <fstream>
25 #include <sstream>
26
27 using namespace std;
28
29 namespace lyx {
30
31 using support::libFileSearch;
32
33 namespace {
34
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_;
52 };
53
54
55 bool Correction::read(idocstream & is)
56 {
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;
72 }
73
74
75 void Correction::write(odocstream & os) const
76 {
77         os << "from: '" << from1_ << "' and '" << from2_
78            << "' to '" << to_ << '\'' << endl;
79 }
80
81
82 bool Correction::correct(MathAtom & at, char_type c) const
83 {
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;
94 }
95
96
97 #if 0
98 idocstream & operator>>(idocstream & is, Correction & corr)
99 {
100         corr.read(is);
101         return is;
102 }
103
104
105 odocstream & operator<<(odocstream & os, Correction & corr)
106 {
107         corr.write(os);
108         return os;
109 }
110 #endif
111
112
113
114 class Corrections {
115 public:
116         ///
117         typedef vector<Correction>::const_iterator const_iterator;
118         ///
119         Corrections() {}
120         ///
121         void insert(const Correction & corr) { data_.push_back(corr); }
122         ///
123         bool correct(MathAtom & at, char_type c) const;
124 private:
125         ///
126         vector<Correction> data_;
127 };
128
129
130 bool Corrections::correct(MathAtom & at, char_type c) const
131 {
132         for (const_iterator it = data_.begin(); it != data_.end(); ++it)
133                 if (it->correct(at, c))
134                         return true;
135         return false;
136 }
137
138
139 Corrections theCorrections;
140
141 void initAutoCorrect()
142 {
143         LYXERR(Debug::MATHED, "reading autocorrect file");
144         support::FileName const file = libFileSearch(string(), "autocorrect");
145         if (file.empty()) {
146                 lyxerr << "Could not find autocorrect file" << endl;
147                 return;
148         }
149
150         string line;
151         ifstream is(file.toFilesystemEncoding().c_str());
152         while (getline(is, line)) {
153                 if (line.empty() || line[0] == '#') {
154                         //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
155                         continue;
156                 }
157                 idocstringstream il(from_utf8(line));
158
159                 //LYXERR(Debug::MATHED, "line '" << line << '\'');
160                 Correction corr;
161                 if (corr.read(il)) {
162                         //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
163                         theCorrections.insert(corr);
164                 }
165         }
166
167         LYXERR(Debug::MATHED, "done reading autocorrections.");
168 }
169
170
171 } // namespace anon
172
173
174 bool math_autocorrect(MathAtom & at, char_type c)
175 {
176         static bool initialized = false;
177
178         if (!initialized) {
179                 initAutoCorrect();
180                 initialized = true;
181         }
182
183         return theCorrections.correct(at, c);
184 }
185
186
187 } // namespace lyx