- // FIXME thailatex does not support the inputenc package, so we
- // ignore switches from/to tis620-0 encoding here. This does of
- // course only work as long as the non-thai text contains ASCII
- // only, but it is the best we can do.
- // Since the \inputencoding command does not work inside sections
- // we ignore the encoding switch also in moving arguments.
- if (((bparams.inputenc == "auto" && !moving_arg) ||
- bparams.inputenc == "default") &&
- oldEnc.name() != newEnc.name() &&
- oldEnc.name() != "ascii" && newEnc.name() != "ascii" &&
- oldEnc.name() != "tis620-0" && newEnc.name() != "tis620-0") {
- LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
- << oldEnc.name() << " to "
- << newEnc.name() << endl;
- os << setEncoding(newEnc.iconvName());
- if (bparams.inputenc != "default") {
- docstring const inputenc(from_ascii(newEnc.latexName()));
- os << "\\inputencoding{" << inputenc << '}';
- return 16 + inputenc.length();
+ Encoding const oldEnc = *runparams.encoding;
+ bool moving_arg = runparams.moving_arg;
+ 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 make_pair(false, 0);
+
+ // FIXME We ignore encoding switches from/to encodings that do
+ // neither support the inputenc package nor the CJK package here.
+ // This does of course only work in special cases (e.g. switch from
+ // tis620-0 to latin1, but the text in latin1 contains ASCII only),
+ // but it is the best we can do
+ if (oldEnc.package() == Encoding::none
+ || newEnc.package() == Encoding::none)
+ return make_pair(false, 0);
+
+ LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
+ << oldEnc.name() << " to " << newEnc.name());
+ os << setEncoding(newEnc.iconvName());
+ if (bparams.inputenc == "default")
+ return make_pair(true, 0);
+
+ docstring const inputenc_arg(from_ascii(newEnc.latexName()));
+ switch (newEnc.package()) {
+ case Encoding::none:
+ // shouldn't ever reach here, see above
+ return make_pair(true, 0);
+ case Encoding::inputenc: {
+ int count = inputenc_arg.length();
+ if (oldEnc.package() == Encoding::CJK &&
+ open_encoding_ == CJK) {
+ os << "\\end{CJK}";
+ open_encoding_ = none;
+ count += 9;
+ }
+ else if (oldEnc.package() == Encoding::inputenc &&
+ open_encoding_ == inputenc) {
+ os << "\\egroup";
+ open_encoding_ = none;
+ count += 7;
+ }
+ if (runparams.local_font != 0 && oldEnc.package() == Encoding::CJK) {
+ // within insets, \inputenc switches need to be
+ // embraced within \bgroup ... \egroup; else CJK fails.
+ os << "\\bgroup";
+ count += 7;
+ open_encoding_ = inputenc;
+ }
+ os << "\\inputencoding{" << inputenc_arg << '}';
+ return make_pair(true, count + 16);
+ }
+ case Encoding::CJK: {
+ int count = inputenc_arg.length();
+ if (oldEnc.package() == Encoding::CJK &&
+ open_encoding_ == CJK) {
+ os << "\\end{CJK}";
+ count += 9;
+ }
+ if (oldEnc.package() == Encoding::inputenc &&
+ open_encoding_ == inputenc) {
+ os << "\\egroup";
+ count += 7;
+ }
+ os << "\\begin{CJK}{" << inputenc_arg << "}{}";
+ open_encoding_ = CJK;
+ return make_pair(true, count + 15);