]> git.lyx.org Git - lyx.git/commitdiff
Fix bug #11586
authorEnrico Forestieri <forenr@lyx.org>
Tue, 25 Jun 2019 20:23:51 +0000 (22:23 +0200)
committerEnrico Forestieri <forenr@lyx.org>
Tue, 25 Jun 2019 20:23:51 +0000 (22:23 +0200)
Do not replace a latex command with the corresponding symbol
in the unicodesymbols file unless it can be encoded in the
document encoding.

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
src/mathed/MathParser.cpp
status.23x

index 52fa0cf0fa831fa4913810bb900519f87d009faa..e911f0babf24bc71897915e824440da90a4de811 100644 (file)
@@ -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, true, 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();
 
index 539b1a3f02faaaf4f109ffd07d098a1503442d32..868b920ebe44224f60ef05172f3a48a45a570cba 100644 (file)
@@ -2077,7 +2077,7 @@ bool Parser::parse1(InsetMathGrid & grid, unsigned flags,
                                                        Encodings::MATH_CMD | Encodings::TEXT_CMD,
                                                        is_combining, termination);
                                        }
-                                       if (c) {
+                                       if (c && buf->params().encoding().encodable(c)) {
                                                if (termination) {
                                                        if (nextToken().cat() == catBegin) {
                                                                getToken();
index 964a5397a5a4492cf546f9b072ce38388630be1c..dbd63f04daf995b583b9971693b3c996c242ab17 100644 (file)
@@ -54,6 +54,10 @@ Avoid using text mode for unicode symbols representable in math mode (bug 9616).
 
 - Avoid superfluous braces in the optional argument of a macro (bug 11552).
 
+- Do not replace a latex command with the corresponding symbol in the
+  unicodesymbols file, unless it can be encoded in the document encoding
+  (bug 11586).
+
 
 * USER INTERFACE