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