+ BufferParams const & bparams = buf.params();
+
+ bool const maintext = text.isMainText();
+ bool const is_child = buf.masterBuffer() != &buf;
+
+ // Open a CJK environment at the beginning of the main buffer
+ // if the document's language is a CJK language
+ // (but not in child documents)
+ if (maintext && !is_child
+ && bparams.encoding().package() == Encoding::CJK) {
+ os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
+ << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
+ open_encoding_ = CJK;
+ }
+ // if "auto begin" is switched off, explicitly switch the
+ // language on at start
+ string const mainlang = runparams.use_polyglossia
+ ? getPolyglossiaEnvName(bparams.language)
+ : bparams.language->babel();
+ string const lang_begin_command = runparams.use_polyglossia ?
+ "\\begin{$$lang}" : lyxrc.language_command_begin;
+
+ if (maintext && !lyxrc.language_auto_begin &&
+ !mainlang.empty()) {
+ // FIXME UNICODE
+ os << from_utf8(subst(lang_begin_command,
+ "$$lang",
+ mainlang));
+ if (runparams.use_polyglossia
+ && !bparams.language->polyglossiaOpts().empty())
+ os << "["
+ << from_ascii(bparams.language->polyglossiaOpts())
+ << "]";
+ os << '\n';
+ }
+
+ ParagraphList const & paragraphs = text.paragraphs();
+ LASSERT(runparams.par_begin <= runparams.par_end, /**/);
+
+ if (runparams.par_begin == runparams.par_end) {
+ // The full doc will be exported but it is easier to just rely on
+ // runparams range parameters that will be passed TeXEnvironment.
+ runparams.par_begin = 0;
+ runparams.par_end = paragraphs.size();
+ }
+
+ pit_type pit = runparams.par_begin;
+ // lastpit is for the language check after the loop.
+ pit_type lastpit = pit;
+ // variables used in the loop: