Hi!

Attached find the (I hope) final version of this patch. I addressed all of the issues which Georg pointed out (Georg, please make sure), and tested this again.

I think that this should go in for 1.5.0 if possible, so if I get another OK (I'm assuming Georg's unless I hear otherwise) I'll put this in, so that we can get it tested as much as possible before the release.

Thanks!
Dov
Index: lyx-devel/src/output_latex.cpp
===================================================================
--- lyx-devel.orig/src/output_latex.cpp 2007-07-02 22:26:31.000000000 +0300
+++ lyx-devel/src/output_latex.cpp      2007-07-05 00:07:14.000000000 +0300
@@ -37,6 +37,8 @@
 
 using std::endl;
 using std::string;
+using std::pair;
+using std::make_pair;
 
 
 namespace {
@@ -295,7 +297,9 @@
                }
        }
 
-       // Switch file encoding if necessary
+       // Switch file encoding if necessary; no need to do this for "default"
+       // encoding, since this only affects the position of the outputted
+       // \inputencoding command; the encoding switch will occur when necessary
        if (bparams.inputenc == "auto" &&
            runparams.encoding->package() == Encoding::inputenc) {
                // Look ahead for future encoding changes.
@@ -313,12 +317,14 @@
                        // encoding to that required by the language of c.
                        Encoding const * const encoding =
                                pit->getFontSettings(bparams, 
i).language()->encoding();
-                       if (encoding->package() == Encoding::inputenc &&
-                           switchEncoding(os, bparams, false,
-                                          *(runparams.encoding), *encoding) > 
0) {
+                       pair<bool, int> enc_switch = switchEncoding(os, 
bparams, false,
+                                       *(runparams.encoding), *encoding);
+                       if (encoding->package() == Encoding::inputenc && 
enc_switch.first) {
                                runparams.encoding = encoding;
-                               os << '\n';
-                               texrow.newline();
+                               if (enc_switch.second > 0) {
+                                       os << '\n';
+                                       texrow.newline();
+                               }
                        }
                        break;
                }
@@ -598,17 +604,17 @@
 }
 
 
-int switchEncoding(odocstream & os, BufferParams const & bparams,
+pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
                   bool moving_arg, Encoding const & oldEnc,
                   Encoding const & newEnc)
 {
-       if ((bparams.inputenc != "auto" || moving_arg)
-               && bparams.inputenc != "default")
-               return 0;
+       if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
+               || moving_arg)
+               return make_pair(false, 0);
 
        // Do nothing if the encoding is unchanged.
        if (oldEnc.name() == newEnc.name())
-               return 0;
+               return make_pair(false, 0);
 
        // FIXME We ignore encoding switches from/to encodings that do
        // neither support the inputenc package nor the CJK package here.
@@ -617,19 +623,20 @@
        // but it is the best we can do
        if (oldEnc.package() == Encoding::none
                || newEnc.package() == Encoding::none)
-               return 0;
+               return make_pair(false, 0);
 
        LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
                << oldEnc.name() << " to "
                << newEnc.name() << endl;
        os << setEncoding(newEnc.iconvName());
        if (bparams.inputenc == "default")
-               return 0;
+               return make_pair(true, 0);
 
        docstring const inputenc(from_ascii(newEnc.latexName()));
        switch (newEnc.package()) {
                case Encoding::none:
-                       return 0;
+                       // shouldn't ever reach here, see above
+                       return make_pair(true, 0);
                case Encoding::inputenc: {
                        int count = inputenc.length();
                        if (oldEnc.package() == Encoding::CJK) {
@@ -637,7 +644,7 @@
                                count += 9;
                        }
                        os << "\\inputencoding{" << inputenc << '}';
-                       return count + 16;
+                       return make_pair(true, count + 16);
                 }
                case Encoding::CJK: {
                        int count = inputenc.length();
@@ -646,11 +653,11 @@
                                count += 9;
                        }
                        os << "\\begin{CJK}{" << inputenc << "}{}";
-                       return count + 15;
+                       return make_pair(true, count + 15);
                }
        }
        // Dead code to avoid a warning:
-       return 0;
+       return make_pair(true, 0);
 }
 
 } // namespace lyx
Index: lyx-devel/src/Paragraph.cpp
===================================================================
--- lyx-devel.orig/src/Paragraph.cpp    2007-07-02 22:26:31.000000000 +0300
+++ lyx-devel/src/Paragraph.cpp 2007-07-05 00:15:35.000000000 +0300
@@ -2044,11 +2044,11 @@
                // Switch file encoding if necessary
                if (runparams.encoding->package() == Encoding::inputenc &&
                    font.language()->encoding()->package() == 
Encoding::inputenc) {
-                       int const count = switchEncoding(os, bparams,
+                       std::pair<bool, int> const enc_switch = 
switchEncoding(os, bparams,
                                        runparams.moving_arg, 
*(runparams.encoding),
                                        *(font.language()->encoding()));
-                       if (count > 0) {
-                               column += count;
+                       if (enc_switch.first) {
+                               column += enc_switch.second;
                                runparams.encoding = 
font.language()->encoding();
                        }
                }
Index: lyx-devel/src/Font.cpp
===================================================================
--- lyx-devel.orig/src/Font.cpp 2007-07-02 22:26:31.000000000 +0300
+++ lyx-devel/src/Font.cpp      2007-07-04 23:18:06.000000000 +0300
@@ -40,6 +40,7 @@
 using std::endl;
 using std::string;
 using std::ostream;
+using std::pair;
 
 #ifndef CXX_GLOBAL_CSTD
 using std::strlen;
@@ -785,12 +786,12 @@
        }
 
        if (language()->encoding()->package() == Encoding::CJK) {
-               int const c = switchEncoding(os, bparams,
+               pair<bool, int> const c = switchEncoding(os, bparams,
                                runparams.moving_arg, *(runparams.encoding),
                                *(language()->encoding()));
-               if (c > 0) {
+               if (c.first) {
                        open_encoding_ = true;
-                       count += c;
+                       count += c.second;
                        runparams.encoding = language()->encoding();
                }
        }
@@ -943,11 +944,11 @@
                // We need to close the encoding even if it does not change
                // to do correct environment nesting
                Encoding const * const ascii = 
encodings.getFromLyXName("ascii");
-               int const c = switchEncoding(os, bparams,
+               pair<bool, int> const c = switchEncoding(os, bparams,
                                runparams.moving_arg, *(runparams.encoding),
                                *ascii);
-               BOOST_ASSERT(c > 0);
-               count += c;
+               BOOST_ASSERT(c.first);
+               count += c.second;
                runparams.encoding = ascii;
                open_encoding_ = false;
        }
Index: lyx-devel/src/output_latex.h
===================================================================
--- lyx-devel.orig/src/output_latex.h   2007-07-02 22:26:31.000000000 +0300
+++ lyx-devel/src/output_latex.h        2007-07-05 00:11:01.000000000 +0300
@@ -12,6 +12,8 @@
 #ifndef OUTPUT_LATEX_H
 #define OUTPUT_LATEX_H
 
+#include <utility>
+
 #include "support/docstream.h"
 
 
@@ -43,10 +45,11 @@
                     std::string const & everypar = std::string());
 
 /// Switch the encoding of \p os from \p oldEnc to \p newEnc if needed.
-/// \return the number of characters written to \p os.
-int switchEncoding(odocstream & os, BufferParams const & bparams,
-                  bool moving_arg, Encoding const & oldEnc,
-                  Encoding const & newEnc);
+/// \return (did the encoding change?, number of characters written to \p os)
+std::pair<bool, int> switchEncoding(odocstream & os, 
+                    BufferParams const & bparams,
+                    bool moving_arg, Encoding const & oldEnc,
+                    Encoding const & newEnc);
 
 } // namespace lyx
 

Reply via email to