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