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