- // XeTeX/LuaTeX use only one encoding per document:
- // * with useNonTeXFonts: "utf8plain",
- // * with XeTeX and TeX fonts: "ascii" (inputenc fails),
- // * with LuaTeX and TeX fonts: only one encoding accepted by luainputenc.
- if (runparams.isFullUnicode())
+ // Never switch encoding with non-TeX fonts (always "utf8plain"),
+ // with LuaTeX and TeX fonts (only one encoding accepted by luainputenc),
+ // or if we're in a moving argument or inherit the outer encoding.
+ if (bparams.useNonTeXFonts
+ || runparams.flavor == OutputParams::LUATEX
+ || runparams.flavor == OutputParams::DVILUATEX
+ || newEnc.name() == "inherit")
+ return make_pair(false, 0);
+
+ // Only switch for auto-selected legacy encodings (inputenc setting
+ // "auto-legacy" or "auto-legacy-plain").
+ // The "listings" environment can force a switch also with other
+ // encoding settings (it does not support variable width encodings
+ // (utf8, jis, ...) under 8-bit latex engines).
+ if (!force && ((bparams.inputenc != "auto-legacy" && bparams.inputenc != "auto-legacy-plain")
+ || runparams.moving_arg))