]> git.lyx.org Git - features.git/blob - src/mathed/MathAutoCorrect.cpp
reduce line noise
[features.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 #include "support/docstream.h"
22
23 #include <fstream>
24 #include <sstream>
25
26
27 namespace lyx {
28
29 using support::libFileSearch;
30
31 using std::string;
32 using std::ifstream;
33 using std::endl;
34 using std::vector;
35
36 namespace {
37
38 class Correction {
39 public:
40         ///
41         Correction() {}
42         ///
43         bool correct(MathAtom & at, char_type c) const;
44         ///
45         bool read(idocstream & is);
46         ///
47         void write(odocstream & os) const;
48 private:
49         ///
50         MathAtom from1_;
51         ///
52         char_type from2_;
53         ///
54         MathAtom to_;
55 };
56
57
58 bool Correction::read(idocstream & is)
59 {
60         docstring s1, s2, s3;
61         is >> s1 >> s2 >> s3;
62         if (!is)
63                 return false;
64         if (s2.size() != 1)
65                 return false;
66         MathData ar1, ar3;
67         mathed_parse_cell(ar1, s1);
68         mathed_parse_cell(ar3, s3);
69         if (ar1.size() != 1 || ar3.size() != 1)
70                 return false;
71         from1_ = ar1.front();
72         from2_ = s2[0];
73         to_    = ar3.front();
74         return true;
75 }
76
77
78 void Correction::write(odocstream & os) const
79 {
80         os << "from: '" << from1_ << "' and '" << from2_
81            << "' to '" << to_ << '\'' << endl;
82 }
83
84
85 bool Correction::correct(MathAtom & at, char_type c) const
86 {
87         //LYXERR(Debug::MATHED,
88         //      "trying to correct ar: " << at << " from: '" << from1_ << '\'');
89         if (from2_ != c)
90                 return false;
91         if (asString(at) != asString(from1_))
92                 return false;
93         LYXERR(Debug::MATHED, "match found! subst in " << at
94                 << " from: '" << from1_ << "' to '" << to_ << '\'');
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");
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 << '\'');
157                         continue;
158                 }
159                 idocstringstream il(from_utf8(line));
160
161                 //LYXERR(Debug::MATHED, "line '" << line << '\'');
162                 Correction corr;
163                 if (corr.read(il)) {
164                         //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
165                         theCorrections.insert(corr);
166                 }
167         }
168
169         LYXERR(Debug::MATHED, "done reading autocorrections.");
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