]> git.lyx.org Git - lyx.git/blob - src/mathed/MathAutoCorrect.cpp
958a12652674226c7a5bbffb0e99541341f25313
[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 #include "support/debug.h"
19
20 #include "support/filetools.h" //  LibFileSearch
21 #include "support/docstream.h"
22
23 #include <fstream>
24 #include <sstream>
25
26 using namespace std;
27
28 namespace lyx {
29
30 using support::libFileSearch;
31
32 namespace {
33
34 class Correction {
35 public:
36         ///
37         Correction() {}
38         ///
39         bool correct(MathAtom & at, char_type c) const;
40         ///
41         bool read(idocstream & is);
42         ///
43         void write(odocstream & os) const;
44 private:
45         ///
46         MathAtom from1_;
47         ///
48         char_type from2_;
49         ///
50         MathAtom to_;
51 };
52
53
54 bool Correction::read(idocstream & is)
55 {
56         docstring s1, s2, s3;
57         is >> s1 >> s2 >> s3;
58         if (!is)
59                 return false;
60         if (s2.size() != 1)
61                 return false;
62         MathData ar1, ar3;
63         mathed_parse_cell(ar1, s1);
64         mathed_parse_cell(ar3, s3);
65         if (ar1.size() != 1 || ar3.size() != 1)
66                 return false;
67         from1_ = ar1.front();
68         from2_ = s2[0];
69         to_    = ar3.front();
70         return true;
71 }
72
73
74 void Correction::write(odocstream & os) const
75 {
76         os << "from: '" << from1_ << "' and '" << from2_
77            << "' to '" << to_ << '\'' << endl;
78 }
79
80
81 bool Correction::correct(MathAtom & at, char_type c) const
82 {
83         //LYXERR(Debug::MATHED,
84         //      "trying to correct ar: " << at << " from: '" << from1_ << '\'');
85         if (from2_ != c)
86                 return false;
87         if (asString(at) != asString(from1_))
88                 return false;
89         LYXERR(Debug::MATHED, "match found! subst in " << at
90                 << " from: '" << from1_ << "' to '" << to_ << '\'');
91         at = to_;
92         return true;
93 }
94
95
96 idocstream & operator>>(idocstream & is, Correction & corr)
97 {
98         corr.read(is);
99         return is;
100 }
101
102
103 odocstream & operator<<(odocstream & os, Correction & corr)
104 {
105         corr.write(os);
106         return os;
107 }
108
109
110
111
112 class Corrections {
113 public:
114         ///
115         typedef vector<Correction>::const_iterator const_iterator;
116         ///
117         Corrections() {}
118         ///
119         void insert(const Correction & corr) { data_.push_back(corr); }
120         ///
121         bool correct(MathAtom & at, char_type c) const;
122 private:
123         ///
124         vector<Correction> data_;
125 };
126
127
128 bool Corrections::correct(MathAtom & at, char_type c) const
129 {
130         for (const_iterator it = data_.begin(); it != data_.end(); ++it)
131                 if (it->correct(at, c))
132                         return true;
133         return false;
134 }
135
136
137 Corrections theCorrections;
138
139 void initAutoCorrect()
140 {
141         LYXERR(Debug::MATHED, "reading autocorrect file");
142         support::FileName const file = libFileSearch(string(), "autocorrect");
143         if (file.empty()) {
144                 lyxerr << "Could not find autocorrect file" << endl;
145                 return;
146         }
147
148         string line;
149         ifstream is(file.toFilesystemEncoding().c_str());
150         while (getline(is, line)) {
151                 if (line.size() == 0 || line[0] == '#') {
152                         //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
153                         continue;
154                 }
155                 idocstringstream il(from_utf8(line));
156
157                 //LYXERR(Debug::MATHED, "line '" << line << '\'');
158                 Correction corr;
159                 if (corr.read(il)) {
160                         //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
161                         theCorrections.insert(corr);
162                 }
163         }
164
165         LYXERR(Debug::MATHED, "done reading autocorrections.");
166 }
167
168
169 } // namespace anon
170
171
172 bool math_autocorrect(MathAtom & at, char c)
173 {
174         static bool initialized = false;
175
176         if (!initialized) {
177                 initAutoCorrect();
178                 initialized = true;
179         }
180
181         return theCorrections.correct(at, c);
182 }
183
184
185 } // namespace lyx