aka "auto correction" for mathed. Type '-' '>' to get '\rightarrow'...
Andre' -- Those who desire to give up Freedom in order to gain Security, will not have, nor do they deserve, either one. (T. Jefferson)
Index: lib/autocorrect =================================================================== RCS file: lib/autocorrect diff -N lib/autocorrect --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/autocorrect 21 May 2002 12:52:17 -0000 @@ -0,0 +1,2 @@ +-> \rightarrow Index: src/mathed/Makefile.am =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/Makefile.am,v retrieving revision 1.88 diff -u -p -r1.88 Makefile.am --- src/mathed/Makefile.am 25 Mar 2002 13:24:28 -0000 1.88 +++ src/mathed/Makefile.am 21 May 2002 12:52:34 -0000 @@ -19,6 +19,8 @@ libmathed_la_SOURCES = \ math_arrayinset.h \ math_atom.C \ math_atom.h \ + math_autocorrect.C \ + math_autocorrect.h \ math_biginset.C \ math_biginset.h \ math_binominset.C \ Index: src/mathed/math_autocorrect.C =================================================================== RCS file: src/mathed/math_autocorrect.C diff -N src/mathed/math_autocorrect.C --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/mathed/math_autocorrect.C 21 May 2002 12:52:35 -0000 @@ -0,0 +1,151 @@ +#include <config.h> + +#include "LString.h" +#include "debug.h" +#include "support/filetools.h" // LibFileSearch +#include "math_data.h" +#include "math_parser.h" + +#include <iostream> +#include <fstream> +#include <map> + +using std::ifstream; +using std::istream; +using std::ostream; +using std::map; + + +namespace { + +class Correction { +public: + /// + Correction() {} + /// + bool correct(MathArray & ar, unsigned int & pos) const; + /// + void read(istream & is); + /// + void write(ostream & os) const; +private: + /// + MathArray from_; + /// + MathArray to_; +}; + + +void Correction::read(istream & is) +{ + string from, to; + is >> from >> to; + if (!is) + return; + from_.clear(); + to_.clear(); + mathed_parse_cell(from_, from); + mathed_parse_cell(to_, to); +} + + +void Correction::write(ostream & os) const +{ + os << "from: '" << from_ << "' to '" << to_ << "'\n"; +} + + +bool Correction::correct(MathArray & ar, unsigned int & pos) const +{ + unsigned int const startpos = pos - ar.size(); + lyxerr[Debug::MATHED] + << "correct ar: " << ar << " from: '" << from_ << "'\n"; + if (!ar.matchpart(from_, startpos)) + return false; + lyxerr[Debug::MATHED] + << "correct " << ar << " from: '" << from_ << "' to '" << to_ << "'\n"; + ar.erase(startpos, pos); + ar.insert(startpos, to_); + pos += from_.size() - to_.size(); + return true; +} + + +istream & operator>>(istream & is, Correction & corr) +{ + corr.read(is); + return is; +} + + +ostream & operator<<(ostream & os, Correction & corr) +{ + corr.write(os); + return os; +} + + + + +class Corrections { +public: + /// + typedef vector<Correction>::const_iterator const_iterator; + /// + Corrections() {} + /// + void insert(const Correction & corr) { data_.push_back(corr); } + /// + bool correct(MathArray & ar, unsigned int & pos) const; +private: + /// + vector<Correction> data_; +}; + + +bool Corrections::correct(MathArray & ar, unsigned int & pos) const +{ + for (const_iterator it = data_.begin(); it != data_.end(); ++it) + if (it->correct(ar, pos)) + return true; + return false; +} + + +Corrections theCorrections; + +void initAutoCorrect() +{ + lyxerr[Debug::MATHED] << "reading autocorrect file\n"; + string const file = LibFileSearch(string(), "autocorrect"); + if (file.empty()) { + lyxerr << "Could not find autocorrect file\n"; + return; + } + + ifstream is(file.c_str()); + Correction corr; + while (is >> corr) + theCorrections.insert(corr); + + lyxerr[Debug::MATHED] << "read autocorrections:\n"; +} + + +} // namespace anon + + +void math_autocorrect(MathArray & ar, unsigned int & pos) +{ + static bool initialized = false; + + if (!initialized) { + initAutoCorrect(); + initialized = true; + } + + theCorrections.correct(ar, pos); + + //WordList::iterator it = theWordList.find(str); + //return (it != theWordList.end()) ? &(it->second) : 0; +} Index: src/mathed/math_autocorrect.h =================================================================== RCS file: src/mathed/math_autocorrect.h diff -N src/mathed/math_autocorrect.h --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ src/mathed/math_autocorrect.h 21 May 2002 12:52:35 -0000 @@ -0,0 +1,9 @@ +#ifndef MATHAUTOCORRECT_H +#define MATHAUTOCORRECT_H + +class MathArray; + +// make "corrections" according to file lib/autocorrect +bool math_autocorrect(MathArray & ar, unsigned int & pos); + +#endif Index: src/mathed/math_cursor.C =================================================================== RCS file: /usr/local/lyx/cvsroot/lyx-devel/src/mathed/math_cursor.C,v retrieving revision 1.261 diff -u -p -r1.261 math_cursor.C --- src/mathed/math_cursor.C 15 May 2002 23:49:08 -0000 1.261 +++ src/mathed/math_cursor.C 21 May 2002 12:52:35 -0000 @@ -29,6 +29,7 @@ #include "Painter.h" #include "math_cursor.h" #include "formulabase.h" +#include "math_autocorrect.h" #include "math_arrayinset.h" #include "math_braceinset.h" #include "math_boxinset.h" @@ -439,6 +440,7 @@ void MathCursor::insert(char c, MathText //lyxerr << "inserting '" << c << "'\n"; selClearOrDel(); plainInsert(MathAtom(new MathCharInset(c, t))); + math_autocorrect(array(), pos()); }