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