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