Dov Feldstern wrote:
Bo Peng wrote:So, something fishy is definitely going on, I'm looking into it now.I think the first thing to do is to figure out the correct latex syntax when other languages are used. Herbert said that only latin language is supported so maybe non-latin language is not supported at all in the listings caption. BoYeah, I've been skimming through the listings manual and I see that some languages (e.g., Hebrew) may be problematic. But I'm not clear on whether that includes the captions or not...For starters, we can focus on the simplest case --- just plain english for everything...
Hi!I've been playing around with this for the past few days, and I sort of understand some of what's going on. Attached is a patch which starts to solve the problem --- it actually appears to already work in its current form for plain English documents, but there's still work that needs to be done in order to get it working within documents with other languages (right now I'm only focusing on having the listing itself in "English", the caption in any *single* additional language besides English, within a document with any number of languages). Theoretically, it should also be possible to allow text in one other language inside the listing, using escape to latex, but for now I'm not trying to implement that part.
What the patch does so far is this:*) Get rid of the hardcoded \inputencoding --- that should never be done that way. For example, it shouldn't be output at all if the document's encoding is set to "default" (Latex default) or to a specific encoding --- the inputenc package isn't even loaded in that case, I think. *) Encoding *is* switched using the switchEncoding mechanism, which is supposed to do the right thing also in the cases mentioned above.
*) Encoding is switched back at the end of the listing. What still needs to be done:*) I think there are problems with the latex output line counting --- I marked one place with a FIXME, but also in other places (where encoding switches happen) I'm not sure it's always done correctly, because sometimes the encoding switch is followed by a newline and sometimes it isn't --- though this may be handled correctly by switchEncoding. *) Language should also be switched: before the inset, we need to make sure the language is set to a language which supports latin1 (most European languages do, but Hebrew, for example, doesn't); after the inset, we need to switch back to the local language. This is generally done within the TeXOnePar function, but since that's not called here, we need to do it explicitly. I don't know how to do it in a general way, and without duplicating code... *) The caption should *not* inherit the font from the listing, but rather from the external font, I think --- both in the GUI and the backend. Right now weird things are happening in this respect --- if you insert a caption, then it is in the normal font in the backend (as it should be, IMO), despite the fact that it appears in typewriter in the GUI. But if you change the language of the text in the caption, then suddenly it's also in typewriter and a different color... This needs to be fixed. *) In order to allow one additional language/encoding within the caption (and/or within the listing itself, in the "escape to latex"), I think it suffices to use the inputencoding optional argument (see the attached example file). I don't know how to do this in the code, yet. Also, within the caption itself, \inputencoding is not allowed. This should be dealt with similarly to how it's dealt with in section headers (see r17827 for hints as to what's done, but I'm not sure where what's described there is actually implemented).
Attached are (1) the patch so far; and (2) an example tex file (hand edited --- not generated by LyX!) and its dvi, which show some of the possibilities: a listing inside a Hebrew document, with a caption which is mixed Hebrew and English, and within the listing itself there are "comments" in Hebrew, using the escape to latex mechanism.
I'd be happy to have some help with what still needs to be done. Dov
Index: lyx-devel/src/insets/InsetListings.cpp =================================================================== --- lyx-devel.orig/src/insets/InsetListings.cpp 2007-08-08 00:21:22.000000000 +0300 +++ lyx-devel/src/insets/InsetListings.cpp 2007-08-08 00:21:23.000000000 +0300 @@ -22,6 +22,8 @@ #include "MetricsInfo.h" #include "Cursor.h" #include "support/lstrings.h" +#include "Buffer.h" +#include "output_latex.h" #include <sstream> @@ -177,11 +179,23 @@ else os << "\\lstinline[" << from_ascii(param_string) << "]" << *delimiter; } else { - docstring const caption = getCaption(buf, runparams); - 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}["; + os << "\n\\begingroup\n"; + 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, the running params have changed + OutputParams rp = runparams; + if (enc_switch.first) + rp.encoding = latex_language->encoding(); + rp.local_font = &text_.real_current_font; + rp.moving_arg = true; + docstring const caption = getCaption(buf, rp); + if (param_string.empty() && caption.empty()) { + os << "\n"; + } else { + os << "["; if (!caption.empty()) { os << "caption={" << caption << '}'; if (!param_string.empty()) @@ -189,14 +203,19 @@ } 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"; + 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;
newfile19.tex
Description: TeX document
newfile19.dvi
Description: TeX dvi file