]> git.lyx.org Git - lyx.git/blob - src/mathed/MathAutoCorrect.cpp
Another assertion that got transformed in 1b1f8dd2
[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 idocstream & operator>>(idocstream & is, Correction & corr)
98 {
99         corr.read(is);
100         return is;
101 }
102
103
104 odocstream & operator<<(odocstream & os, Correction & corr)
105 {
106         corr.write(os);
107         return os;
108 }
109
110
111
112
113 class Corrections {
114 public:
115         ///
116         typedef vector<Correction>::const_iterator const_iterator;
117         ///
118         Corrections() {}
119         ///
120         void insert(const Correction & corr) { data_.push_back(corr); }
121         ///
122         bool correct(MathAtom & at, char_type c) const;
123 private:
124         ///
125         vector<Correction> data_;
126 };
127
128
129 bool Corrections::correct(MathAtom & at, char_type c) const
130 {
131         for (const_iterator it = data_.begin(); it != data_.end(); ++it)
132                 if (it->correct(at, c))
133                         return true;
134         return false;
135 }
136
137
138 Corrections theCorrections;
139
140 void initAutoCorrect()
141 {
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;
147         }
148
149         string line;
150         ifstream is(file.toFilesystemEncoding().c_str());
151         while (getline(is, line)) {
152                 if (line.empty() || line[0] == '#') {
153                         //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
154                         continue;
155                 }
156                 idocstringstream il(from_utf8(line));
157
158                 //LYXERR(Debug::MATHED, "line '" << line << '\'');
159                 Correction corr;
160                 if (corr.read(il)) {
161                         //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
162                         theCorrections.insert(corr);
163                 }
164         }
165
166         LYXERR(Debug::MATHED, "done reading autocorrections.");
167 }
168
169
170 } // namespace anon
171
172
173 bool math_autocorrect(MathAtom & at, char_type c)
174 {
175         static bool initialized = false;
176
177         if (!initialized) {
178                 initAutoCorrect();
179                 initialized = true;
180         }
181
182         return theCorrections.correct(at, c);
183 }
184
185
186 } // namespace lyx