Dear Isaac,

I forward your e-mail to the developers list because this is the right place to discuss. I understand that we forgot to keep you in cc: of the discussion. To avoid this in the future (although we'll try to do our best), I propose two solutions:
* that you subscribe to the list
* or that you create tickets on www.lyx.org/trac for the different subjects that are outlines below.

Next, I'll write about the patches, which are the interesting part :)

JMarc


-------- Message transféré --------
Sujet :         Re: [PATCH] Make math autocorrrect work with more than 2 chars
Date :  Fri, 12 Apr 2019 03:23:43 +0000
De :    Isaac Oscar Gariano <isaacos...@live.com.au>
Pour : skost...@lyx.org <skost...@lyx.org>, lasgout...@lyx.org <lasgout...@lyx.org>



Dear Lyx Developers,
Thank you JMarc and scott for responding! For some reason I did not receiver an email, so I only noticed by looking at www.mail-archive.com <http://www.mail-archive.com/>.
As suggested I have split my patch into two commits, and attached them.

The main reason I made this change was because I am a frequent Micrsofot Word user, however at work I do not have windows and am usually required to write things following LaTeX styles, hence I wanted to use lyx (which works great!). However, I relly heavily on Microsoft Word's math equation features as I use it's WYSYWIG features to work out what an equation should be as writing it, rather than just typeseting an equation I allready have in my head. In particular its autocorrect has several features that the lyx one is lacking:

  * It supports autocorrect from an arbitrarilly long sequence of
    unicode characters.
  * It supports undoing the autocorrect with Ctrl-Z
  * It supports unicode input. LyX seems supports this as well, but when
    I put '→' in the autocorrect file instead of '\rightarrow' it shows
    in lyx with a different font, however it still outputs \rightarrow
    in the LaTeX. It would be nice if I could change the font lyx uses
    here, so I could at least copy and paste unicode symbols from other
    programs.
  * It has a simple graphical dialogue for edditing the autocorrect
    list. I use this feature quite frequently, so it would be very
    annoying to have to restart the program every time I make a simple
    change to it. I wanted to implement such a thing for LyX, however I
    am not familar with QT, so I decided to be lazy and just add a
    reload button. Perhaps instead we should just have a 'reload config'
    button that reloads all config files at once, similar to the
    existing 'reconfigure' button?
  * It allows turning on autocorrect in text mode, thus can be quite
    convenient. It may be better add a feature to LyX to make such
    autocorrects automatically create an equation, e.g. typing '->' in a
    text field would generate the latex '$\rightarrow$.
  * It triggers autocorrect on space, this is very annoying, lyx on the
    other hand applies the autocorrect immediatley!
  * It allows verbatim spaces, which is annoying as I am unable to pass
    literal spaces to a user-defined math macro, though it is possible
    with the predefined \text macro; I may consider 'fixing' this later.

As for the commented out texmacs corrections, I havn't had the time to go through it, but their are several problems with them:

  * Some refer to commands that lyx dosn't recognise, like \longdashv (I
    don't know how to fix this problem).
  * They are in the wrong format (they are missing whitespace before the
    character that triggers the redirect)
  * Prefixes to some of them are allready registered as autocorrects
    (e.g. '<=**> \lesseqqgtr' needs to be changed to '\leqq > \lesseqqgtr')


As for your question:
> Where do I find the first and last command of autocorrect in the code?The idea of extending it is very nice IMO. I'm not sure what you mean by this? Are asking where the code that performs the autoccrect is? If so most of the changes are in 'src/mathed/MathAutoCorrect.cpp'. The main work for autocorrection is done by Correction::correct, and the main work for reading the autocorrect list is done in Correction::read. The rest of the changes were just parsing a cursor to Correction::correct instead of a MathAtom and making the Correction class store MathDatas instead of MathAtoms.

If your asking the fileformat of the autocorrect file: every line starting with  'whitespace* #' is a comment; the rest of the lines are of the form 'MathAtom1+ whitespace+ Character  whitespace+ MathAtom2+'; This means 'MathAtom1+ character' is autocorrected to 'MathAtom2+'; note that autocorrects are tested from first to last in the file, so if one is a suffix of the other, e.g. '- > \rightarrrow' and '-- > \longrightarrow', the longer one should appear in the file /first/.

— Isaac

From 5e0506adabeedb6ad8045fb41172f0a670d39225 Mon Sep 17 00:00:00 2001
From: Isaac <isaacos...@hotmail.com>
Date: Tue, 9 Apr 2019 13:59:52 +1200
Subject: [PATCH] Added button to reload math autocorrections

---
 src/frontends/qt4/GuiPrefs.cpp           |  7 +++++++
 src/frontends/qt4/GuiPrefs.h             |  1 +
 src/frontends/qt4/ui/PrefCompletionUi.ui |  8 ++++++++
 src/mathed/MathAutoCorrect.cpp           | 17 ++++++++++++-----
 src/mathed/MathAutoCorrect.h             |  1 +
 5 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/src/frontends/qt4/GuiPrefs.cpp b/src/frontends/qt4/GuiPrefs.cpp
index 5c9575d699..bcad9b3740 100644
--- a/src/frontends/qt4/GuiPrefs.cpp
+++ b/src/frontends/qt4/GuiPrefs.cpp
@@ -40,6 +40,7 @@
 #include "Session.h"
 #include "SpellChecker.h"
 
+#include "mathed/MathAutoCorrect.h"
 #include "support/debug.h"
 #include "support/FileName.h"
 #include "support/filetools.h"
@@ -588,6 +589,12 @@ void PrefCompletion::on_popupTextCB_clicked()
        enableCB();
 }
 
+void PrefCompletion::on_reloadAutocorrectionsPB_clicked()
+{
+       reload_autocorrections();
+}
+
+
 
 void PrefCompletion::enableCB()
 {
diff --git a/src/frontends/qt4/GuiPrefs.h b/src/frontends/qt4/GuiPrefs.h
index df16fde51b..b73cd428a0 100644
--- a/src/frontends/qt4/GuiPrefs.h
+++ b/src/frontends/qt4/GuiPrefs.h
@@ -198,6 +198,7 @@ public:
 private Q_SLOTS:
        void on_popupTextCB_clicked();
        void on_inlineTextCB_clicked();
+       void on_reloadAutocorrectionsPB_clicked();
 };
 
 
diff --git a/src/frontends/qt4/ui/PrefCompletionUi.ui 
b/src/frontends/qt4/ui/PrefCompletionUi.ui
index 45faee48d4..6b98f87691 100644
--- a/src/frontends/qt4/ui/PrefCompletionUi.ui
+++ b/src/frontends/qt4/ui/PrefCompletionUi.ui
@@ -62,6 +62,13 @@
         </property>
        </widget>
       </item>
+      <item>
+       <widget class="QPushButton" name="reloadAutocorrectionsPB">
+        <property name="text">
+         <string>Reload Auto&amp;corrections</string>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
@@ -317,6 +324,7 @@
   <tabstop>inlineMathCB</tabstop>
   <tabstop>popupMathCB</tabstop>
   <tabstop>autocorrectionCB</tabstop>
+  <tabstop>reloadAutocorrectionsPB</tabstop>
  </tabstops>
  <includes>
   <include location="local">qt_i18n.h</include>
diff --git a/src/mathed/MathAutoCorrect.cpp b/src/mathed/MathAutoCorrect.cpp
index d25f4ea7f5..2692b474cf 100644
--- a/src/mathed/MathAutoCorrect.cpp
+++ b/src/mathed/MathAutoCorrect.cpp
@@ -132,6 +132,7 @@ public:
        void insert(const Correction & corr) { data_.push_back(corr); }
        ///
        bool correct(Cursor & cur, char_type c) const;
+       void clear() { data_.clear(); }
 private:
        ///
        vector<Correction> data_;
@@ -148,9 +149,11 @@ bool Corrections::correct(Cursor & cur, char_type c) const
 
 
 Corrections theCorrections;
+bool initialized = false;
 
 void initAutoCorrect()
 {
+       initialized = true;
        LYXERR(Debug::MATHED, "reading autocorrect file");
        support::FileName const file = libFileSearch(string(), "autocorrect");
        if (file.empty()) {
@@ -184,13 +187,17 @@ void initAutoCorrect()
 
 bool math_autocorrect(Cursor & cur, char_type c)
 {
-       static bool initialized = false;
-
-       if (!initialized) {
+       if (!initialized)
                initAutoCorrect();
-               initialized = true;
-       }
 
        return theCorrections.correct(cur, c);
 }
+
+void reload_autocorrections()
+{
+       if (initialized)
+               theCorrections.clear();
+
+       initAutoCorrect();
+}
 } // namespace lyx
diff --git a/src/mathed/MathAutoCorrect.h b/src/mathed/MathAutoCorrect.h
index a782d678ee..e74f4bc13a 100644
--- a/src/mathed/MathAutoCorrect.h
+++ b/src/mathed/MathAutoCorrect.h
@@ -20,6 +20,7 @@ class Cursor;
 
 // make "corrections" according to file lib/autocorrect
 bool math_autocorrect(Cursor & cur, char_type c);
+void reload_autocorrections();
 
 } // namespace lyx
 
-- 
2.20.1

From 4ab710fffc469a195566cd76f4ad9d19cd44fb1b Mon Sep 17 00:00:00 2001
From: Isaac <isaacos...@hotmail.com>
Date: Tue, 9 Apr 2019 13:48:46 +1200
Subject: [PATCH] Make math autocorrrect work with more than 2 chars

---
 src/mathed/InsetMathNest.cpp   |  2 +-
 src/mathed/MathAutoCorrect.cpp | 44 ++++++++++++++++++++--------------
 src/mathed/MathAutoCorrect.h   |  4 ++--
 3 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/src/mathed/InsetMathNest.cpp b/src/mathed/InsetMathNest.cpp
index 798ca8920b..85e4dd7c70 100644
--- a/src/mathed/InsetMathNest.cpp
+++ b/src/mathed/InsetMathNest.cpp
@@ -1868,7 +1868,7 @@ bool InsetMathNest::interpretChar(Cursor & cur, char_type 
const c)
 
        // try auto-correction
        if (lyxrc.autocorrection_math && cur.autocorrect() && cur.pos() != 0
-                 && math_autocorrect(cur.prevAtom(), c))
+                 && math_autocorrect(cur, c))
                return true;
 
        // no special circumstances, so insert the character without any fuss
diff --git a/src/mathed/MathAutoCorrect.cpp b/src/mathed/MathAutoCorrect.cpp
index fc4ad440d2..d25f4ea7f5 100644
--- a/src/mathed/MathAutoCorrect.cpp
+++ b/src/mathed/MathAutoCorrect.cpp
@@ -10,6 +10,7 @@
 
 #include <config.h>
 
+#include "Cursor.h"
 #include "MathAutoCorrect.h"
 #include "MathData.h"
 #include "InsetMath.h"
@@ -38,18 +39,18 @@ public:
        /// \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_;
 };
 
 
@@ -64,26 +65,35 @@ bool Correction::read(idocstream & is)
        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;
 }
 
 
-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_ << '\'');
        if (from2_ != c)
                return false;
-       if (asString(at) != asString(from1_))
+       pos_type n = from1_.size();
+       if (cur.pos() < from1_.size()) // not enough to match
                return false;
-       LYXERR(Debug::MATHED, "match found! subst in " << at
+       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_ << '\'');
-       at = to_;
+
+       for (pos_type i = 0; i < n; i++)
+               cur.backspace(true);
+
+       cur.insert(to_);
        return true;
 }
 
@@ -121,17 +131,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;
 }
@@ -172,7 +182,7 @@ void initAutoCorrect()
 } // namespace
 
 
-bool math_autocorrect(MathAtom & at, char_type c)
+bool math_autocorrect(Cursor & cur, char_type c)
 {
        static bool initialized = false;
 
@@ -181,8 +191,6 @@ bool math_autocorrect(MathAtom & at, char_type c)
                initialized = true;
        }
 
-       return theCorrections.correct(at, c);
+       return theCorrections.correct(cur, c);
 }
-
-
 } // namespace lyx
diff --git a/src/mathed/MathAutoCorrect.h b/src/mathed/MathAutoCorrect.h
index 679e83738e..a782d678ee 100644
--- a/src/mathed/MathAutoCorrect.h
+++ b/src/mathed/MathAutoCorrect.h
@@ -16,10 +16,10 @@
 
 namespace lyx {
 
-class MathAtom;
+class Cursor;
 
 // make "corrections" according to file lib/autocorrect
-bool math_autocorrect(MathAtom & at, char_type c);
+bool math_autocorrect(Cursor & cur, char_type c);
 
 } // namespace lyx
 
-- 
2.20.1

Reply via email to