Dov Feldstern wrote:
Juergen Spitzmueller wrote:
Jean-Marc Lasgouttes wrote:

Juergen, could you tell me when setting local_font is necessary? I did
not see the problem when creating a new listing inset with caption.

The problem showed up when you created a listings inset with a caption
before the patch: somehow (and I never actually found out where/why) the
caption inherits the language and encoding of the listings insets, which is
latex/latin1 (latin1, because listings fails with utf8; the hardcoding is
certainly not ideal). Therefore, the caption was embedded in
\encoding{latin1}...\selectlanguage{english} (or whatever the document
language is).
I forced the caption to use the buffer font (buffer()->params().getFont()) in metrics and draw, which solved the display issue (which is now correctly
roman, not typewriter), but not the language issue (which is apparently
still latex).

What the code in InsetListings does, is to force the params.local_font to
real_current_font, before the params are passed to
InsetCaption::getArgument, which removes the wrong encoding and language
switches.

I agree that this smells fishy, but I didn't find another way to solve the
issue (which results in LaTeX errors).
(Dov might know better; I stole the code from his patch).

Any input highly appreciated.

Jürgen



Hi!

I've been looking at this again in the past few days, and I'm making some progress, but I will also need some help with it.

AFAIU, there are two issues: one is the fact that the caption's font in a listing inset was being set in the *GUI* to typewriter --- Jurgen's patch seems to have fixed that. The second is the latex output of listings in general, and specifically this was causing a problem with the caption because of the encodings.

I don't know anything about the GUI end of things. But regarding the latex output, I more or less understand what's going on.

Much of the problem is that the listing's latex method is doing all of the latex output from scratch --- i.e., isn't going through TeXOnePar --- so the handling of languages and encodings which is normally handled there is not being applied in the case of listings. So we have to apply it "manually" in the case of listings.

My previous patch handled the encoding --- I still think it should be applied in full (I will send in an updated patch soon). Jurgen had said he applied only those parts he understood, so I gather that the rest was not understood well enough --- I will try to comment it better now, and if it's still not clear, I'll be happy to try to explain it.

The handling of the language is more complicated --- mainly because this is handled directly in TeXOnePar, and not in a separate function as switchEncoding is. So what I'm trying to do now is to factor out the language code into separate functions, and then I'll be able to reuse them more easily in the listings inset.

What I could use help with right now is this:
1) What exactly is TexRow? In TeXOnePar it's used right in the middle of all the language stuff, but in the listings inset it doesn't even exist. So I'm trying to figure out whether or not I need to pass it in the the language function that I'm trying to factor out... 2) Why is the line_count necessary in the latex output methods? I see that sometimes we do and sometimes we don't keep track of it, and even where we do --- I'm not sure that it really matters if it's correct or not. Does it matter?

JMarc --- to your question about local_font: I believe that the reason we are setting it is only so that when the latex generation continues *after* the listing, it will know what was done inside the listing --- which may affect whether or not another encoding/language switch is needed again (since we may change these inside the listing, they may have to be switched again coming out). But I'm not sure about this. I'm also not sure how this should be dealt with in trunk now that we don't have the text object. I'm first trying to fix this in branch, then I'll see how to deal with it in trunk...

Dov


I'm having a lot of trouble with this --- I keep discovering more and more ways in which the listings inset does not interact well with other languages...

I'm attaching a series of patches which are a step in the right direction, but still not there yet. However, I think that they only make things better, so I would be happy to have them applied once someone looks them over...

1. Currently, when inserting a caption in a listing the default font is set to latex_language also in the caption (because that's the active font when the caption is created). The patch listings-fix_caption_font.patch solves this issue, by setting the default font in the caption to the font that is active in the containing paragraph at the position where the Listings inset is.

[this applies cleanly to both branch and trunk]

2. Currently, the \inputencoding of the inset is written in a hard-coded fashion. There are a few problems with this: first of all, if the document encoding is "auto" or a specific encoding, then inputencoding is not used at all, and if it's output to the latex file it will cause errors. Secondly, if an encoding switch is really needed (because, say the encoding before the inset is non-latin), then it's not enough to tell latex about the encoding switch: LyX itself has to know about it, so that it can write the latex file correctly. This issue is fixed by the patch listings-encoding.patch, which is basically the rest of my original patch.

[this applies cleanly to branch, but not yet to trunk, because of the current_font issue from the previous part of the patch]

3. As we know, the encoding in a Listings inset has to be latin1. We are already setting it correctly (see item 2). However, this is not enough: we must also make sure that the current language setting is one which supports latin1. My problem here is that I don't know how I can tell whether a given language supports latin1 or not. So my idea is to just set the language to English, which I know supports latin1. This means that if someone is writing in a french document, then before the listing the language will be set to English. This should not be a problem, because the text of the listing itself is output verbatim, so the declared language really doesn't make a difference. In order to correctly output the language transitions, I've factored out the language switch commands from TeXOnePar, so that they can be reused by the listing. The refactoring is in the attached patch language_commands_refactoring.patch. If someone could like it over, I'd like to apply it to trunk so that it can be tested, and if it doesn't seem to cause trouble then it can be applied to branch as well. But I would not apply to branch right away, because it touches all language switches, not just the Listings inset.

4. Now that we have language transition commands available, we can use them in the Listings inset, to make sure that the language is set to a language which supports latin1. I've started doing this in the attached
listings-language.patch. However, it's still not working perfectly:

One problem I'm having (which has nothing to do with the patches, it's a problem which already exists, I believe): when loading a file, it looks like current_font and real_current_font in the insets are not being set correctly. Although they should be set to latex_language, they are not. As soon as you move the cursor into the listing, however, then this problem is fixed. But if you have 20 listings, you don't want to have to move the cursor through all of them each time. I don't know where this problem is coming from, and it's also hard to observe --- you need to get into a debugger to really see that it's happening. But this means that language transitions are not being done correctly, because the current language inside the listing is not identified correctly.

Another problem is that if the only time english is used is in the listings (as per patches 3-4), then it's not getting loaded at the beginning of the file. That's why I added the useLanguage in validate(), but now it seems to be loading the languages in the wrong order.

I can provide more details about both of these issues later, but (a) I first want to hear some feedback in general (is there any point in pursuing this at all?), and (b) I just don't have more time at the moment :( .

Comments are welcome. Note that 4 depends on 3, but other than that the patches are independent; although they are all necessary, I think...

Dov
diff -r 6015dfc708c2 src/insets/InsetListings.cpp
--- a/src/insets/InsetListings.cpp      Fri Sep 28 15:39:48 2007 +0200
+++ b/src/insets/InsetListings.cpp      Fri Sep 28 15:40:13 2007 +0200
@@ -247,6 +247,24 @@ void InsetListings::doDispatch(Cursor & 
                InsetERT::doDispatch(cur, cmd);
                break;
        }
+       case LFUN_CAPTION_INSERT: {
+               // We intercept this so that the caption does not inherit the 
+               // latex_language from the listings inset. Rather, we use the 
font
+               // that is active at the inset's position in the containing 
paragraph.
+               CursorSlice const & containing_slice = 
+                       cur[cur.depth() - 2]; // -2 because depth is 1-based 
+               Font font = containing_slice.paragraph().getFontSettings(
+                       cur.buffer().params(), containing_slice.pos());
+               // Let the caption be created by the normal dispatch mechanism
+               InsetERT::doDispatch(cur, cmd);
+               // When the caption was created by doDispatch, the cursor was 
moved 
+               // into the newly created inset. So now get the text_ object 
inside
+               // the inset, and set its font.
+               cur.paragraph().inInset()->asTextInset()->text_.current_font = 
+                       
cur.paragraph().inInset()->asTextInset()->text_.real_current_font =
+                               font;
+               break;
+       }
        default:
                InsetERT::doDispatch(cur, cmd);
                break;
diff -r 2cd7948ac07d src/insets/InsetListings.cpp
--- a/src/insets/InsetListings.cpp      Wed Sep 26 22:31:50 2007 +0200
+++ b/src/insets/InsetListings.cpp      Thu Sep 27 22:36:14 2007 +0200
@@ -22,6 +22,8 @@
 #include "MetricsInfo.h"
 #include "Cursor.h"
 #include "support/lstrings.h"
+#include "Buffer.h"
+#include "output_latex.h"
 
 #include <sstream>
 
@@ -177,15 +179,28 @@ int InsetListings::latex(Buffer const & 
                else
                        os << "\\lstinline[" << from_ascii(param_string) << "]" 
<< *delimiter;
        } else {
+               os << "\n\\begingroup\n";
+               // Listings contents must be encoded in latin1
+               std::pair<bool, int> enc_switch = 
+                       switchEncoding(os, buf.params(), runparams.moving_arg, 
+                               *(runparams.encoding), 
*(latex_language->encoding()));
+               os << "\n\\begin{lstlisting}";
+               lines += (2 + enc_switch.second);
+               // Going into the caption, running params should represent the 
current
+               // state within the listings inset, which may be different from 
what 
+               // it was outside (because we just changed the encoding, for 
example) 
                OutputParams rp = runparams;
+               if (enc_switch.first)
+                       rp.encoding = latex_language->encoding();
                rp.local_font = &text_.real_current_font;
                rp.moving_arg = true;
+               // Generate the caption's latex text...
                docstring const caption = getCaption(buf, rp);
-               runparams.encoding = rp.encoding;
-               if (param_string.empty() && caption.empty())
-                       os << 
"\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}\n";
-               else {
-                       os << 
"\n\\begingroup\n\\inputencoding{latin1}\n\\begin{lstlisting}[";
+               // ... and output it as a listings param, along with any other 
params
+               if (param_string.empty() && caption.empty()) {
+                       os << "\n";
+               } else {
+                       os << "[";
                        if (!caption.empty()) {
                                os << "caption={" << caption << '}';
                                if (!param_string.empty())
@@ -193,14 +208,20 @@ int InsetListings::latex(Buffer const & 
                        }
                        os << from_utf8(param_string) << "]\n";
                }
-               lines += 4;
+               // FIXME: the line counting is off; caption may use more than 
one line..?
+               lines++;
        }
        os << code;
        if (lstinline)
                os << *delimiter;
        else {
-               os << "\n\\end{lstlisting}\n\\endgroup\n";
-               lines += 3;
+               os << "\n\\end{lstlisting}\n";
+               // After closing the inset, set the encoding back to what it 
was 
+               std::pair<bool, int> enc_switch = 
+                       switchEncoding(os, buf.params(), runparams.moving_arg,
+                               *(latex_language->encoding()), 
*(runparams.encoding));
+               os << "\\endgroup\n";
+               lines += (2 + enc_switch.second);
        }
 
        return lines;
diff -Npru lyx-15x.2cd7948ac07d/src/output_latex.cpp 
lyx-15x/src/output_latex.cpp
--- lyx-15x.2cd7948ac07d/src/output_latex.cpp   2007-09-28 15:31:39.202943427 
+0200
+++ lyx-15x/src/output_latex.cpp        2007-09-28 15:31:39.206943863 +0200
@@ -58,7 +58,6 @@ TeXOnePar(Buffer const & buf,
          OutputParams const & runparams,
          string const & everypar = string());
 
-
 ParagraphList::const_iterator
 TeXDeeper(Buffer const & buf,
          ParagraphList const & paragraphs,
@@ -104,30 +103,8 @@ TeXEnvironment(Buffer const & buf,
                (pit != paragraphs.begin())
                ? boost::prior(pit)->getParLanguage(bparams)
                : doc_language;
-       if (par_language->babel() != prev_par_language->babel()) {
-
-               if (!lyxrc.language_command_end.empty() &&
-                   prev_par_language->babel() != doc_language->babel() &&
-                   !prev_par_language->babel().empty()) {
-                       os << from_ascii(subst(
-                               lyxrc.language_command_end,
-                               "$$lang",
-                               prev_par_language->babel()))
-                          << '\n';
-                       texrow.newline();
-               }
-
-               if ((lyxrc.language_command_end.empty() ||
-                    par_language->babel() != doc_language->babel()) &&
-                   !par_language->babel().empty()) {
-                       os << from_ascii(subst(
-                               lyxrc.language_command_begin,
-                               "$$lang",
-                               par_language->babel()))
-                          << '\n';
-                       texrow.newline();
-               }
-       }
+       openLanguage(par_language, prev_par_language, doc_language, 
+               os, runparams, texrow);
 
        bool leftindent_open = false;
        if (!pit->params().leftIndent().zero()) {
@@ -275,77 +252,15 @@ TeXOnePar(Buffer const & buf,
                (pit != paragraphs.begin()) ?
                        boost::prior(pit)->getParLanguage(bparams) : 
outer_language;
 
-       if (par_language->babel() != prev_language->babel()
-           // check if we already put language command in TeXEnvironment()
-           && !(style->isEnvironment()
+       if (// check if we already put language command in TeXEnvironment()
+           !(style->isEnvironment()
                 && (pit == paragraphs.begin() ||
                     (boost::prior(pit)->layout() != pit->layout() &&
                      boost::prior(pit)->getDepth() <= pit->getDepth())
                     || boost::prior(pit)->getDepth() < pit->getDepth())))
        {
-               if (!lyxrc.language_command_end.empty() &&
-                   prev_language->babel() != outer_language->babel() &&
-                   !prev_language->babel().empty())
-               {
-                       os << from_ascii(subst(lyxrc.language_command_end,
-                               "$$lang",
-                               prev_language->babel()))
-                          << '\n';
-                       texrow.newline();
-               }
-
-               // We need to open a new language if we couldn't close the 
previous 
-               // one (because there's no language_command_end); and even if 
we closed
-               // the previous one, if the current language is different than 
the
-               // outer_language (which is currently in effect once the 
previous one
-               // is closed).
-               if ((lyxrc.language_command_end.empty() ||
-                    par_language->babel() != outer_language->babel()) &&
-                   !par_language->babel().empty()) {
-                       // If we're inside an inset, and that inset is within 
an \L or \R
-                       // (or equivalents), then within the inset, too, any 
opposite
-                       // language paragraph should appear within an \L or \R 
(in addition
-                       // to, outside of, the normal language switch commands).
-                       // This behavior is not correct for ArabTeX, though.
-                       if (    // not for ArabTeX
-                                       (par_language->lang() != 
"arabic_arabtex" &&
-                                        outer_language->lang() != 
"arabic_arabtex") &&
-                                       // are we in an inset?
-                                       runparams.local_font != 0 &&
-                                       // is the inset within an \L or \R?
-                                       // 
-                                       // FIXME: currently, we don't check 
this; this means that
-                                       // we'll have unnnecessary \L and \R 
commands, but that 
-                                       // doesn't seem to hurt (though latex 
will complain)
-                                       // 
-                                       // is this paragraph in the opposite 
direction?
-                                       runparams.local_font->isRightToLeft() !=
-                                               par_language->rightToLeft()
-                               ) {
-                               // FIXME: I don't have a working copy of the 
Arabi package, so
-                               // I'm not sure if the farsi and arabic_arabi 
stuff is correct
-                               // or not...
-                               if (par_language->lang() == "farsi")
-                                       os << "\\textFR{";
-                               else if (outer_language->lang() == "farsi")
-                                       os << "\\textLR{";
-                               else if (par_language->lang() == "arabic_arabi")
-                                       os << "\\textAR{";
-                               else if (outer_language->lang() == 
"arabic_arabi")
-                                       os << "\\textLR{";
-                               // remaining RTL languages currently is hebrew
-                               else if (par_language->rightToLeft())
-                                       os << "\\R{";
-                               else
-                                       os << "\\L{";
-                       }
-                       os << from_ascii(subst(
-                               lyxrc.language_command_begin,
-                               "$$lang",
-                               par_language->babel()))
-                          << '\n';
-                       texrow.newline();
-               }
+               openLanguage(par_language, prev_language, outer_language,
+                       os, runparams, texrow);
        }
 
        // Switch file encoding if necessary; no need to do this for "default"
@@ -511,53 +426,12 @@ TeXOnePar(Buffer const & buf,
        // Closing the language is needed for the last paragraph; it is also
        // needed if we're within an \L or \R that we may have opened above (not
        // necessarily in this paragraph) and are about to close.
-       bool closing_rtl_ltr_environment = 
-               // not for ArabTeX
-               (par_language->lang() != "arabic_arabtex" &&
-                outer_language->lang() != "arabic_arabtex") &&
-               // have we opened and \L or \R environment?
-               runparams.local_font != 0 &&
-               runparams.local_font->isRightToLeft() != 
par_language->rightToLeft() &&
-               // are we about to close the language?
-               ((boost::next(pit) != paragraphs.end() &&
-                 par_language->babel() != 
-                       (boost::next(pit)->getParLanguage(bparams))->babel()) ||
-                (boost::next(pit) == paragraphs.end() &&
-                 par_language->babel() != outer_language->babel()));
-
-       if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
-           && par_language->babel() != outer_language->babel())) {
-               // Since \selectlanguage write the language to the aux file,
-               // we need to reset the language at the end of footnote or
-               // float.
-
-               if (pending_newline) {
-                       os << '\n';
-                       texrow.newline();
-               }
-               if (lyxrc.language_command_end.empty()) {
-                       if (!prev_language->babel().empty()) {
-                               os << from_ascii(subst(
-                                       lyxrc.language_command_begin,
-                                       "$$lang",
-                                       prev_language->babel()));
-                               pending_newline = true;
-                       }
-               } else if (!par_language->babel().empty()) {
-                       os << from_ascii(subst(
-                               lyxrc.language_command_end,
-                               "$$lang",
-                               par_language->babel()));
-                       pending_newline = true;
-               }
-       }
-       if (closing_rtl_ltr_environment)
-               os << "}";
-
-       if (pending_newline) {
-               os << '\n';
-               texrow.newline();
-       }
+       
+       Language const * const next_language = 
+               (boost::next(pit) != paragraphs.end()) ? 
+                       (boost::next(pit)->getParLanguage(bparams)) : 0;
+       closeLanguage(prev_language, par_language, next_language, 
outer_language,
+               os, runparams, texrow, pending_newline);
 
        // If this is the last paragraph, and a local_font was set upon entering
        // the inset, the encoding should be set back to that local_font's 
@@ -747,4 +621,144 @@ pair<bool, int> switchEncoding(odocstrea
        return make_pair(true, 0);
 }
 
+
+// Write language switch to latex output
+int openLanguage(Language const * par_language,
+               Language const * prev_language,
+               Language const * outer_language,
+               odocstream & os, OutputParams const & runparams,
+               TexRow & texrow) {
+
+       int count = 0;
+
+       if (par_language->babel() != prev_language->babel()) {
+
+               if (!lyxrc.language_command_end.empty() &&
+                       prev_language->babel() != outer_language->babel() &&
+                       !prev_language->babel().empty())
+               {
+                       os << from_ascii(subst(lyxrc.language_command_end,
+                               "$$lang",
+                               prev_language->babel()))
+                          << '\n';
+                       count++;
+                       texrow.newline();
+               }
+
+               // We need to open a new language if we couldn't close the 
previous 
+               // one (because there's no language_command_end); and even if 
we closed
+               // the previous one, if the current language is different than 
the
+               // outer_language (which is currently in effect once the 
previous one
+               // is closed).
+               if ((lyxrc.language_command_end.empty() ||
+                        par_language->babel() != outer_language->babel()) &&
+                       !par_language->babel().empty()) {
+                       // If we're inside an inset, and that inset is within 
an \L or \R
+                       // (or equivalents), then within the inset, too, any 
opposite
+                       // language paragraph should appear within an \L or \R 
(in addition
+                       // to, outside of, the normal language switch commands).
+                       // This behavior is not correct for ArabTeX, though.
+                       if (    // not for ArabTeX
+                                       (par_language->lang() != 
"arabic_arabtex" &&
+                                        outer_language->lang() != 
"arabic_arabtex") &&
+                                       // are we in an inset?
+                                       runparams.local_font != 0 &&
+                                       // is the inset within an \L or \R?
+                                       // 
+                                       // FIXME: currently, we don't check 
this; this means that
+                                       // we'll have unnnecessary \L and \R 
commands, but that 
+                                       // doesn't seem to hurt (though latex 
will complain)
+                                       // 
+                                       // is this paragraph in the opposite 
direction?
+                                       runparams.local_font->isRightToLeft() !=
+                                               par_language->rightToLeft()
+                               ) {
+                               // FIXME: I don't have a working copy of the 
Arabi package, so
+                               // I'm not sure if the farsi and arabic_arabi 
stuff is correct
+                               // or not...
+                               if (par_language->lang() == "farsi")
+                                       os << "\\textFR{";
+                               else if (outer_language->lang() == "farsi")
+                                       os << "\\textLR{";
+                               else if (par_language->lang() == "arabic_arabi")
+                                       os << "\\textAR{";
+                               else if (outer_language->lang() == 
"arabic_arabi")
+                                       os << "\\textLR{";
+                               // remaining RTL languages currently is hebrew
+                               else if (par_language->rightToLeft())
+                                       os << "\\R{";
+                               else
+                                       os << "\\L{";
+                       }
+                       os << from_ascii(subst(
+                               lyxrc.language_command_begin,
+                               "$$lang",
+                               par_language->babel()))
+                          << '\n';
+                       texrow.newline();
+                       count++;
+               }
+       }
+
+       return count;
+}
+
+       
+// Close the language 
+void closeLanguage(Language const * prev_language,
+               Language const * par_language,
+               Language const * next_language,
+               Language const * outer_language,
+               odocstream & os, OutputParams const & runparams,
+               TexRow & texrow, bool & pending_newline) {
+
+       bool closing_rtl_ltr_environment = 
+               // not for ArabTeX
+               (par_language->lang() != "arabic_arabtex" &&
+                outer_language->lang() != "arabic_arabtex") &&
+               // have we opened and \L or \R environment?
+               runparams.local_font != 0 &&
+               runparams.local_font->isRightToLeft() != 
par_language->rightToLeft() &&
+               // are we about to close the language?
+               (((next_language != 0) && 
+                 par_language->babel() != next_language->babel()) ||
+                ((next_language == 0) &&
+                 par_language->babel() != outer_language->babel()));
+
+       if (closing_rtl_ltr_environment || 
+               (next_language == 0 && 
+                       par_language->babel() != outer_language->babel())) {
+               // Since \selectlanguage write the language to the aux file,
+               // we need to reset the language at the end of footnote or
+               // float.
+
+               if (pending_newline) {
+                       os << '\n';
+                       texrow.newline();
+               }
+               if (lyxrc.language_command_end.empty()) {
+                       if (!prev_language->babel().empty()) {
+                               os << from_ascii(subst(
+                                       lyxrc.language_command_begin,
+                                       "$$lang",
+                                       prev_language->babel()));
+                               pending_newline = true;
+                       }
+               } else if (!par_language->babel().empty()) {
+                       os << from_ascii(subst(
+                               lyxrc.language_command_end,
+                               "$$lang",
+                               par_language->babel()));
+                       pending_newline = true;
+               }
+       }
+       if (closing_rtl_ltr_environment)
+               os << "}";
+
+       if (pending_newline) {
+               os << '\n';
+               texrow.newline();
+       }
+}
+
 } // namespace lyx
diff -Npru lyx-15x.2cd7948ac07d/src/output_latex.h lyx-15x/src/output_latex.h
--- lyx-15x.2cd7948ac07d/src/output_latex.h     2007-09-28 15:31:39.206943863 
+0200
+++ lyx-15x/src/output_latex.h  2007-09-28 15:31:39.206943863 +0200
@@ -26,6 +26,7 @@ class Paragraph;
 class ParagraphList;
 class OutputParams;
 class TexRow;
+class Language;
 
 /// Export up to \p number optarg insets
 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
@@ -51,6 +52,19 @@ std::pair<bool, int> switchEncoding(odoc
                     bool moving_arg, Encoding const & oldEnc,
                     Encoding const & newEnc);
 
+int openLanguage(Language const * par_language,
+               Language const * prev_language,
+               Language const * outer_language,
+               odocstream & os, OutputParams const & runparams,
+               TexRow & texrow);
+
+void closeLanguage(Language const * prev_language,
+               Language const * par_language,
+               Language const * next_language,
+               Language const * outer_language,
+               odocstream & os, OutputParams const & runparams,
+               TexRow & texrow, bool & pending_newline);
+
 } // namespace lyx
 
 #endif
diff -r 7e2a249be178 src/Language.cpp
--- a/src/Language.cpp  Fri Sep 28 10:59:14 2007 +0200
+++ b/src/Language.cpp  Fri Sep 28 13:03:49 2007 +0200
@@ -33,14 +33,14 @@ Language const * default_language;
 Language const * default_language;
 Language ignore_lang("ignore", "ignore", "Ignore", false, "", 0, "ignore", "");
 Language const * ignore_language = &ignore_lang;
-Language latex_lang("latex", "", "Latex", false, "", 0, "latex", "");
+Language latex_lang("latex", "english", "Latex", false, "", 0, "latex", "");
 Language const * latex_language = &latex_lang;
 
 
 void Languages::read(support::FileName const & filename)
 {
        // We need to set the encoding of latex_lang
-       latex_lang = Language("latex", "", "Latex", false, "iso8859-1",
+       latex_lang = Language("latex", "english", "Latex", false, "iso8859-1",
                              encodings.getFromLyXName("iso8859-1"),
                              "latex", "");
 
diff -r 7e2a249be178 src/insets/InsetListings.cpp
--- a/src/insets/InsetListings.cpp      Fri Sep 28 10:59:14 2007 +0200
+++ b/src/insets/InsetListings.cpp      Fri Sep 28 13:03:49 2007 +0200
@@ -23,7 +23,10 @@
 #include "Cursor.h"
 #include "support/lstrings.h"
 #include "Buffer.h"
+#include "BufferParams.h"
+#include "Buffer.h"
 #include "output_latex.h"
+#include "TexRow.h"
 
 #include <sstream>
 
@@ -143,6 +146,17 @@ int InsetListings::latex(Buffer const & 
        docstring code;
        ParagraphList::const_iterator par = paragraphs().begin();
        ParagraphList::const_iterator end = paragraphs().end();
+
+       // Make sure the language is set correctly (we must use a language 
which 
+       // supports latin1 encoding). Although latex_language is not really a 
babel
+       // language, we have set (in Language.cpp) its babel language to a 
legal 
+       // babel language, so it's OK to use it here as the language.
+       Language const * const par_language = latex_language;
+       Language const * const outer_language = (runparams.local_font != 0) ?
+               runparams.local_font->language() : buf.params().language;
+       TexRow texrow;
+       lines += openLanguage(par_language, outer_language, outer_language,
+               os, runparams, texrow);
 
        while (par != end) {
                pos_type siz = par->size();
@@ -224,6 +238,10 @@ int InsetListings::latex(Buffer const & 
                lines += (2 + enc_switch.second);
        }
 
+       bool pending_newline = false;
+       // Finally, set the language back to what it was
+       closeLanguage(outer_language, par_language, 0, outer_language,
+               os, runparams, texrow, pending_newline);
        return lines;
 }
 
@@ -321,6 +339,7 @@ void InsetListings::validate(LaTeXFeatur
 void InsetListings::validate(LaTeXFeatures & features) const
 {
        features.require("listings");
+       features.useLanguage(latex_language);
        InsetERT::validate(features);
 }
 

Reply via email to