]> git.lyx.org Git - lyx.git/blob - src/mathed/math_autocorrect.C
remove unneeded member
[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         if (s2.size() != 1)
50                 return false;
51         MathArray ar1, ar3;
52         mathed_parse_cell(ar1, s1);
53         mathed_parse_cell(ar3, s3);
54         if (ar1.size() != 1 || ar3.size() != 1)
55                 return false;
56         from1_ = ar1.front();
57         from2_ = s2[0];
58         to_    = ar3.front();
59         return true;
60 }
61
62
63 void Correction::write(ostream & os) const
64 {
65         os << "from: '" << from1_ << "' and '" << from2_
66            << "' to '" << to_ << "'" << endl;
67 }
68
69
70 bool Correction::correct(MathAtom & at, char c) const
71 {
72         //lyxerr[Debug::MATHED]
73         //      << "trying to correct ar: " << at << " from: '" << from1_ << "'" << endl;
74         if (from2_ != c)
75                 return false;
76         if (!at->match(from1_.nucleus()))
77                 return false;
78         lyxerr[Debug::MATHED]
79                 << "match found! subst in " << at
80                 << " from: '" << from1_ << "' to '" << to_ << "'" << endl;
81         at = to_;
82         return true;
83 }
84
85
86 istream & operator>>(istream & is, Correction & corr)
87 {
88         corr.read(is);
89         return is;
90 }
91
92
93 ostream & operator<<(ostream & os, Correction & corr)
94 {
95         corr.write(os);
96         return os;
97 }
98
99
100
101
102 class Corrections {
103 public:
104         ///
105         typedef vector<Correction>::const_iterator const_iterator;
106         ///
107         Corrections() {}
108         ///
109         void insert(const Correction & corr) { data_.push_back(corr); }
110         ///
111         bool correct(MathAtom & at, char c) const;
112 private:
113         ///
114         vector<Correction> data_;
115 };
116
117
118 bool Corrections::correct(MathAtom & at, char c) const
119 {
120         for (const_iterator it = data_.begin(); it != data_.end(); ++it)
121                 if (it->correct(at, c))
122                         return true;
123         return false;
124 }
125
126
127 Corrections theCorrections;
128
129 void initAutoCorrect()
130 {
131         lyxerr[Debug::MATHED] << "reading autocorrect file" << endl;
132         string const file = LibFileSearch(string(), "autocorrect");
133         if (file.empty()) {
134                 lyxerr << "Could not find autocorrect file" << endl;
135                 return;
136         }
137
138         string line;
139         ifstream is(file.c_str());
140         while (getline(is, line)) {
141                 if (line.size() == 0 || line[0] == '#') {
142                         //lyxerr[Debug::MATHED] << "ignoring line '" << line << "'" << endl;
143                         continue;
144                 }
145                 istringstream il(line);
146                 //lyxerr[Debug::MATHED] << "line '" << line << "'" << endl;
147                 Correction corr;
148                 if (corr.read(il)) {
149                         //lyxerr[Debug::MATHED] << "parsed: '" << corr << "'" << endl;
150                         theCorrections.insert(corr);
151                 }
152         }
153
154         lyxerr[Debug::MATHED] << "done reading autocorrections." << endl;
155 }
156
157
158 } // namespace anon
159
160
161 bool math_autocorrect(MathAtom & at, char c)
162 {
163         static bool initialized = false;
164
165         if (!initialized) {
166                 initAutoCorrect();
167                 initialized = true;
168         }
169
170         return theCorrections.correct(at, c);
171 }