commit 15f4fa452d92d0f96a92f84901b2e26ba8d395c8
Author: Enrico Forestieri <[email protected]>
Date:   Sun May 26 09:56:34 2019 +0200

    Supplement the fix for bug #11586
    
    The mhchem \ce inset is a text mode environment but allows entering
    spaces and mathmode commands. However, even if it doesn't allow unicode
    symbols, LyX allows entering them (by copy/paste, for example), causing
    latex errors. As a unicode symbol may have a proper latex representation
    from the unicodesymbols file, use it instead of the bare symbol. Here, we
    don't care about the mode because both text and math mode should be allowed.
    For example, the ⟶ symbol is not recognized but its latex representation
    (\longrightarrow) is fine. Of course, there may be symbols that are
    not recognized anyway, but this is better because they cause explicit
    errors from mhchem instead of cryptic iconv errors in case they cannot
    be represented in the document encoding.
---
 src/mathed/MathExtern.cpp |   37 ++++++++++++++++++++++++++++++++++++-
 1 files changed, 36 insertions(+), 1 deletions(-)

diff --git a/src/mathed/MathExtern.cpp b/src/mathed/MathExtern.cpp
index 323ea13..24f2672 100644
--- a/src/mathed/MathExtern.cpp
+++ b/src/mathed/MathExtern.cpp
@@ -1421,11 +1421,46 @@ void write(MathData const & dat, WriteStream & wi)
 
 void writeString(docstring const & s, WriteStream & os)
 {
-       if (!os.latex() || os.lockedMode()) {
+       if (!os.latex()) {
                os << (os.asciiOnly() ? escape(s) : s);
                return;
        }
 
+       if (os.lockedMode()) {
+               bool space;
+               docstring cmd;
+               for (char_type c : s) {
+                       try {
+                               Encodings::latexMathChar(c, false, 
os.encoding(), cmd, space);
+                               os << cmd;
+                               os.pendingSpace(space);
+                       } catch (EncodingException const & e) {
+                               switch (os.output()) {
+                               case WriteStream::wsDryrun: {
+                                       os << "<" << _("LyX Warning: ")
+                                          << _("uncodable character") << " '";
+                                       os << docstring(1, e.failed_char);
+                                       os << "'>";
+                                       break;
+                               }
+                               case WriteStream::wsPreview: {
+                                       // indicate the encoding error by a 
boxed '?'
+                                       os << "{\\fboxsep=1pt\\fbox{?}}";
+                                       LYXERR0("Uncodable character" << " '"
+                                               << docstring(1, e.failed_char)
+                                               << "'");
+                                       break;
+                               }
+                               case WriteStream::wsDefault:
+                               default:
+                                       // throw again
+                                       throw(e);
+                               }
+                       }
+               }
+               return;
+       }
+
        docstring::const_iterator cit = s.begin();
        docstring::const_iterator end = s.end();
 

Reply via email to