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