]> git.lyx.org Git - lyx.git/blob - src/mathed/MathAutoCorrect.cpp
Remove unused method in ArgumentProxy
[lyx.git] / src / mathed / MathAutoCorrect.cpp
1 /**
2  * \file MathAutoCorrect.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "MathAutoCorrect.h"
14 #include "MathData.h"
15 #include "InsetMath.h"
16 #include "MathSupport.h"
17 #include "MathParser.h"
18
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h" //  LibFileSearch
22 #include "support/docstream.h"
23
24 #include <fstream>
25 #include <sstream>
26
27 using namespace std;
28
29 namespace lyx {
30
31 using support::libFileSearch;
32
33 namespace {
34
35 class Correction {
36 public:
37         ///
38         /// \brief Correction
39         // it is fine not to initialize from2_
40         // coverity[UNINIT_CTOR]
41         Correction() {}
42         ///
43         bool correct(MathAtom & at, char_type c) const;
44         ///
45         bool read(idocstream & is);
46         ///
47         void write(odocstream & os) const;
48 private:
49         ///
50         MathAtom from1_;
51         ///
52         char_type from2_;
53         ///
54         MathAtom to_;
55 };
56
57
58 bool Correction::read(idocstream & is)
59 {
60         docstring s1, s2, s3;
61         is >> s1 >> s2 >> s3;
62         if (!is)
63                 return false;
64         if (s2.size() != 1)
65                 return false;
66         MathData ar1, ar3;
67         mathed_parse_cell(ar1, s1);
68         mathed_parse_cell(ar3, s3);
69         if (ar1.size() != 1 || ar3.size() != 1)
70                 return false;
71         from1_ = ar1.front();
72         from2_ = s2[0];
73         to_    = ar3.front();
74         return true;
75 }
76
77
78 bool Correction::correct(MathAtom & at, char_type c) const
79 {
80         //LYXERR(Debug::MATHED,
81         //      "trying to correct ar: " << at << " from: '" << from1_ << '\'');
82         if (from2_ != c)
83                 return false;
84         if (asString(at) != asString(from1_))
85                 return false;
86         LYXERR(Debug::MATHED, "match found! subst in " << at
87                 << " from: '" << from1_ << "' to '" << to_ << '\'');
88         at = to_;
89         return true;
90 }
91
92
93 #if 0
94 void Correction::write(odocstream & os) const
95 {
96         os << "from: '" << from1_ << "' and '" << from2_
97            << "' to '" << to_ << '\'' << endl;
98 }
99
100
101 idocstream & operator>>(idocstream & is, Correction & corr)
102 {
103         corr.read(is);
104         return is;
105 }
106
107
108 odocstream & operator<<(odocstream & os, Correction & corr)
109 {
110         corr.write(os);
111         return os;
112 }
113 #endif
114
115
116
117 class Corrections {
118 public:
119         ///
120         typedef vector<Correction>::const_iterator const_iterator;
121         ///
122         Corrections() {}
123         ///
124         void insert(const Correction & corr) { data_.push_back(corr); }
125         ///
126         bool correct(MathAtom & at, char_type c) const;
127 private:
128         ///
129         vector<Correction> data_;
130 };
131
132
133 bool Corrections::correct(MathAtom & at, char_type c) const
134 {
135         for (const_iterator it = data_.begin(); it != data_.end(); ++it)
136                 if (it->correct(at, c))
137                         return true;
138         return false;
139 }
140
141
142 Corrections theCorrections;
143
144 void initAutoCorrect()
145 {
146         LYXERR(Debug::MATHED, "reading autocorrect file");
147         support::FileName const file = libFileSearch(string(), "autocorrect");
148         if (file.empty()) {
149                 lyxerr << "Could not find autocorrect file" << endl;
150                 return;
151         }
152
153         string line;
154         ifstream is(file.toFilesystemEncoding().c_str());
155         while (getline(is, line)) {
156                 if (line.empty() || line[0] == '#') {
157                         //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
158                         continue;
159                 }
160                 idocstringstream il(from_utf8(line));
161
162                 //LYXERR(Debug::MATHED, "line '" << line << '\'');
163                 Correction corr;
164                 if (corr.read(il)) {
165                         //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
166                         theCorrections.insert(corr);
167                 }
168         }
169
170         LYXERR(Debug::MATHED, "done reading autocorrections.");
171 }
172
173
174 } // namespace anon
175
176
177 bool math_autocorrect(MathAtom & at, char_type c)
178 {
179         static bool initialized = false;
180
181         if (!initialized) {
182                 initAutoCorrect();
183                 initialized = true;
184         }
185
186         return theCorrections.correct(at, c);
187 }
188
189
190 } // namespace lyx