On Thu, Jun 02, 2011 at 11:36:08PM +0200, Enrico Forestieri wrote:

> On Thu, Jun 02, 2011 at 03:56:42PM -0400, Richard Heck wrote:
> 
> > I think so. I've had a look at it, and it's more difficult than it might
> > seem. A comment in the code (this is output_latex.cpp, lines 770ff)
> > suggests that sometimes we do need to restore the language, whereas it
> > seems that in this sort of case we do not. That suggests we need a
> > layout tag.
> 
> I am not sure it's so difficult. Note that \selectlanguage is used when the
> language is changed at the start of a paragraph, otherwise \foreignlanguage
> is used. In the given example, a workaround is inserting {} in ERT right
> before "word" and then changing the language of "word" only. You can see
> that \foreignlanguage is then employed. Maybe \selectlanguage could be
> used at the start of a paragraph only if Text::isMainText() is true.

See attached patch.

-- 
Enrico
Index: src/output_latex.cpp
===================================================================
--- src/output_latex.cpp        (revisione 38933)
+++ src/output_latex.cpp        (copia locale)
@@ -86,6 +86,8 @@ static TeXEnvironmentData prepareEnviron
 
        BufferParams const & bparams = buf.params();
 
+       bool const maintext = text.isMainText();
+
        // FIXME This test should not be necessary.
        // We should perhaps issue an error if it is.
        Layout const & style = text.inset().forcePlainLayout() ?
@@ -118,9 +120,12 @@ static TeXEnvironmentData prepareEnviron
        string const doc_lang = use_pg ?
                getPolyglossiaEnvName(doc_language) : doc_language->babel();
        string const lang_begin_command = use_pg ?
-               "\\begin{$$lang}" : lyxrc.language_command_begin;
+               "\\begin{$$lang}" : maintext ? lyxrc.language_command_begin
+                                            : lyxrc.language_command_local;
        string const lang_end_command = use_pg ?
-               "\\end{$$lang}" : lyxrc.language_command_end;
+               "\\end{$$lang}" : maintext ? lyxrc.language_command_end : "}";
+
+       bool const breakline = maintext || use_pg;
 
        if (par_lang != prev_par_lang) {
                if (!lang_end_command.empty() &&
@@ -129,9 +134,10 @@ static TeXEnvironmentData prepareEnviron
                        os << from_ascii(subst(
                                lang_end_command,
                                "$$lang",
-                               prev_par_lang))
-                         // the '%' is necessary to prevent unwanted whitespace
-                         << "%\n";
+                               prev_par_lang));
+                       if (breakline)
+                               // break line but prevent unwanted whitespace
+                               os << safebreakln;
                }
 
                if ((lang_end_command.empty() ||
@@ -146,8 +152,9 @@ static TeXEnvironmentData prepareEnviron
                                        os << "["
                                           << 
from_ascii(data.par_language->polyglossiaOpts())
                                           << "]";
-                         // the '%' is necessary to prevent unwanted whitespace
-                       os << "%\n";
+                       if (breakline)
+                               // break line but prevent unwanted whitespace
+                               os << safebreakln;
                }
        }
 
@@ -518,9 +525,12 @@ void TeXOnePar(Buffer const & buf,
        string const outer_lang = use_pg ?
                getPolyglossiaEnvName(outer_language) : outer_language->babel();
        string const lang_begin_command = use_pg ?
-               "\\begin{$$lang}" : lyxrc.language_command_begin;
+               "\\begin{$$lang}" : maintext ? lyxrc.language_command_begin
+                                            : lyxrc.language_command_local;
        string const lang_end_command = use_pg ?
-               "\\end{$$lang}" : lyxrc.language_command_end;
+               "\\end{$$lang}" : maintext ? lyxrc.language_command_end : "}";
+
+       bool const breakline = maintext || use_pg;
 
        if (par_lang != prev_lang
                // check if we already put language command in TeXEnvironment()
@@ -535,9 +545,10 @@ void TeXOnePar(Buffer const & buf,
                {
                        os << from_ascii(subst(lang_end_command,
                                "$$lang",
-                               prev_lang))
-                          // the '%' is necessary to prevent unwanted 
whitespace
-                          << "%\n";
+                               prev_lang));
+                       if (breakline)
+                               // break line but prevent unwanted whitespace
+                               os << safebreakln;
                }
 
                // We need to open a new language if we couldn't close the 
previous
@@ -594,8 +605,9 @@ void TeXOnePar(Buffer const & buf,
                                                os << "["
                                                  << 
from_ascii(par_language->polyglossiaOpts())
                                                  << "]";
-                                  // the '%' is necessary to prevent unwanted 
whitespace
-                               os << "%\n";
+                               if (breakline)
+                                       // break line but prevent unwanted 
whitespace
+                                       os << safebreakln;
                        }
                }
        }
@@ -649,9 +661,11 @@ void TeXOnePar(Buffer const & buf,
                                        os << from_ascii(subst(
                                                lang_begin_command,
                                                "$$lang",
-                                               par_lang))
-                                       // the '%' is necessary to prevent 
unwanted whitespace
-                                       << "%\n";
+                                               par_lang));
+                                       if (breakline)
+                                           // break line but prevent
+                                           // unwanted whitespace
+                                           os << safebreakln;
                                }
                                runparams.encoding = encoding;
                        }
@@ -798,7 +812,7 @@ void TeXOnePar(Buffer const & buf,
                                        lang_end_command,
                                        "$$lang",
                                        par_lang));
-                               pending_newline = true;
+                               pending_newline = breakline;
                        }
                }
        }

Reply via email to