2 * \file MathAutoCorrect.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
13 #include "MathAutoCorrect.h"
15 #include "InsetMath.h"
16 #include "MathSupport.h"
17 #include "MathParser.h"
19 #include "support/debug.h"
20 #include "support/FileName.h"
21 #include "support/filetools.h" // LibFileSearch
22 #include "support/docstream.h"
31 using support::libFileSearch
;
40 bool correct(MathAtom
& at
, char_type c
) const;
42 bool read(idocstream
& is
);
44 void write(odocstream
& os
) const;
55 bool Correction::read(idocstream
& is
)
64 mathed_parse_cell(ar1
, s1
);
65 mathed_parse_cell(ar3
, s3
);
66 if (ar1
.size() != 1 || ar3
.size() != 1)
75 void Correction::write(odocstream
& os
) const
77 os
<< "from: '" << from1_
<< "' and '" << from2_
78 << "' to '" << to_
<< '\'' << endl
;
82 bool Correction::correct(MathAtom
& at
, char_type c
) const
84 //LYXERR(Debug::MATHED,
85 // "trying to correct ar: " << at << " from: '" << from1_ << '\'');
88 if (asString(at
) != asString(from1_
))
90 LYXERR(Debug::MATHED
, "match found! subst in " << at
91 << " from: '" << from1_
<< "' to '" << to_
<< '\'');
97 idocstream
& operator>>(idocstream
& is
, Correction
& corr
)
104 odocstream
& operator<<(odocstream
& os
, Correction
& corr
)
116 typedef vector
<Correction
>::const_iterator const_iterator
;
120 void insert(const Correction
& corr
) { data_
.push_back(corr
); }
122 bool correct(MathAtom
& at
, char_type c
) const;
125 vector
<Correction
> data_
;
129 bool Corrections::correct(MathAtom
& at
, char_type c
) const
131 for (const_iterator it
= data_
.begin(); it
!= data_
.end(); ++it
)
132 if (it
->correct(at
, c
))
138 Corrections theCorrections
;
140 void initAutoCorrect()
142 LYXERR(Debug::MATHED
, "reading autocorrect file");
143 support::FileName
const file
= libFileSearch(string(), "autocorrect");
145 lyxerr
<< "Could not find autocorrect file" << endl
;
150 ifstream
is(file
.toFilesystemEncoding().c_str());
151 while (getline(is
, line
)) {
152 if (line
.size() == 0 || line
[0] == '#') {
153 //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
156 idocstringstream
il(from_utf8(line
));
158 //LYXERR(Debug::MATHED, "line '" << line << '\'');
161 //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
162 theCorrections
.insert(corr
);
166 LYXERR(Debug::MATHED
, "done reading autocorrections.");
173 bool math_autocorrect(MathAtom
& at
, char_type c
)
175 static bool initialized
= false;
182 return theCorrections
.correct(at
, c
);