]> git.lyx.org Git - lyx.git/blobdiff - src/mathed/MathAutoCorrect.cpp
Display equation/theorem numbers in insert cross reference dialog.
[lyx.git] / src / mathed / MathAutoCorrect.cpp
index c275a9341fa071ae663b78c98be38620c83a21c0..5d5a7d098c4782057603ac8e373f64529052cbae 100644 (file)
@@ -3,7 +3,7 @@
  * This file is part of LyX, the document processor.
  * Licence details can be found in the file COPYING.
  *
- * \author André Pönitz
+ * \author André Pönitz
  *
  * Full author contact details are available in file CREDITS.
  */
 #include <config.h>
 
 #include "MathAutoCorrect.h"
+
+#include "Cursor.h"
 #include "MathData.h"
 #include "InsetMath.h"
 #include "MathSupport.h"
 #include "MathParser.h"
-#include "debug.h"
 
+#include "support/debug.h"
+#include "support/FileName.h"
 #include "support/filetools.h" //  LibFileSearch
+#include "support/docstream.h"
 
 #include <fstream>
 #include <sstream>
 
+using namespace std;
 
 namespace lyx {
 
 using support::libFileSearch;
 
-using std::string;
-using std::ifstream;
-using std::endl;
-using std::vector;
-
 namespace {
 
 class Correction {
 public:
        ///
-       Correction() {}
+       /// \brief Correction
+       Correction() : from2_(0) {}
        ///
-       bool correct(MathAtom & at, char_type c) const;
+       bool correct(Cursor & cur, char_type c) const;
        ///
        bool read(idocstream & is);
        ///
        void write(odocstream & os) const;
 private:
        ///
-       MathAtom from1_;
+       MathData from1_;
        ///
        char_type from2_;
        ///
-       MathAtom to_;
+       MathData to_;
 };
 
 
@@ -62,41 +63,59 @@ bool Correction::read(idocstream & is)
                return false;
        if (s2.size() != 1)
                return false;
-       MathArray ar1, ar3;
+       MathData ar1, ar3;
        mathed_parse_cell(ar1, s1);
        mathed_parse_cell(ar3, s3);
-       if (ar1.size() != 1 || ar3.size() != 1)
-               return false;
-       from1_ = ar1.front();
+       from1_ = ar1;
        from2_ = s2[0];
-       to_    = ar3.front();
+       to_    = ar3;
        return true;
 }
 
 
-void Correction::write(odocstream & os) const
-{
-       os << "from: '" << from1_ << "' and '" << from2_
-          << "' to '" << to_ << '\'' << endl;
-}
-
-
-bool Correction::correct(MathAtom & at, char_type c) const
+bool Correction::correct(Cursor & cur, char_type c) const
 {
-       //LYXERR(Debug::MATHED)
-       //      << "trying to correct ar: " << at << " from: '" << from1_ << '\'' << endl;
+       //LYXERR(Debug::MATHED,
+       //      "trying to correct ar: " << at << " from: '" << from1_ << '\'');
        if (from2_ != c)
                return false;
-       if (asString(at) != asString(from1_))
+       pos_type n = from1_.size();
+       if (cur.pos() < pos_type(from1_.size())) // not enough to match
                return false;
-       LYXERR(Debug::MATHED)
-               << "match found! subst in " << at
-               << " from: '" << from1_ << "' to '" << to_ << '\'' << endl;
-       at = to_;
+       pos_type start = cur.pos() - from1_.size();
+
+       for (pos_type i = 0; i < n; i++)
+               if (asString(cur.cell()[start + i]) != asString(from1_[i]))
+                       return false;
+
+       LYXERR(Debug::MATHED, "match found! subst in " << cur.cell()
+               << " from: '" << from1_ << "' to '" << to_ << '\'');
+
+       /* To allow undoing the completion, we proceed in 4 steps
+        * - inset the raw character
+        * - split undo group so that we have two separate undo actions
+        * - record undo, delete the character we just entered and the from1_ part
+        * - finally, do the insertion of the correction.
+        */
+       cur.insert(c);
+       cur.splitUndoGroup();
+       cur.recordUndoSelection();
+       cur.cell().erase(cur.pos() - n - 1, cur.pos());
+       cur.pos() -= n + 1;
+
+       cur.insert(to_);
        return true;
 }
 
 
+#if 0
+void Correction::write(odocstream & os) const
+{
+       os << "from: '" << from1_ << "' and '" << from2_
+          << "' to '" << to_ << '\'' << endl;
+}
+
+
 idocstream & operator>>(idocstream & is, Correction & corr)
 {
        corr.read(is);
@@ -109,7 +128,7 @@ odocstream & operator<<(odocstream & os, Correction & corr)
        corr.write(os);
        return os;
 }
-
+#endif
 
 
 
@@ -122,17 +141,17 @@ public:
        ///
        void insert(const Correction & corr) { data_.push_back(corr); }
        ///
-       bool correct(MathAtom & at, char_type c) const;
+       bool correct(Cursor & cur, char_type c) const;
 private:
        ///
        vector<Correction> data_;
 };
 
 
-bool Corrections::correct(MathAtom & at, char_type c) const
+bool Corrections::correct(Cursor & cur, char_type c) const
 {
        for (const_iterator it = data_.begin(); it != data_.end(); ++it)
-               if (it->correct(at, c))
+               if (it->correct(cur, c))
                        return true;
        return false;
 }
@@ -142,7 +161,7 @@ Corrections theCorrections;
 
 void initAutoCorrect()
 {
-       LYXERR(Debug::MATHED) << "reading autocorrect file" << endl;
+       LYXERR(Debug::MATHED, "reading autocorrect file");
        support::FileName const file = libFileSearch(string(), "autocorrect");
        if (file.empty()) {
                lyxerr << "Could not find autocorrect file" << endl;
@@ -152,28 +171,28 @@ void initAutoCorrect()
        string line;
        ifstream is(file.toFilesystemEncoding().c_str());
        while (getline(is, line)) {
-               if (line.size() == 0 || line[0] == '#') {
-                       //LYXERR(Debug::MATHED) << "ignoring line '" << line << '\'' << endl;
+               if (line.empty() || line[0] == '#') {
+                       //LYXERR(Debug::MATHED, "ignoring line '" << line << '\'');
                        continue;
                }
                idocstringstream il(from_utf8(line));
 
-               //LYXERR(Debug::MATHED) << "line '" << line << '\'' << endl;
+               //LYXERR(Debug::MATHED, "line '" << line << '\'');
                Correction corr;
                if (corr.read(il)) {
-                       //LYXERR(Debug::MATHED) << "parsed: '" << corr << '\'' << endl;
+                       //LYXERR(Debug::MATHED, "parsed: '" << corr << '\'');
                        theCorrections.insert(corr);
                }
        }
 
-       LYXERR(Debug::MATHED) << "done reading autocorrections." << endl;
+       LYXERR(Debug::MATHED, "done reading autocorrections.");
 }
 
 
-} // namespace anon
+} // namespace
 
 
-bool math_autocorrect(MathAtom & at, char c)
+bool math_autocorrect(Cursor & cur, char_type c)
 {
        static bool initialized = false;
 
@@ -182,8 +201,6 @@ bool math_autocorrect(MathAtom & at, char c)
                initialized = true;
        }
 
-       return theCorrections.correct(at, c);
+       return theCorrections.correct(cur, c);
 }
-
-
 } // namespace lyx