2 * \file output_latex.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
8 * Full author contact details are available in file CREDITS.
13 #include "output_latex.h"
16 #include "BufferParams.h"
19 #include "InsetList.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "ParagraphParameters.h"
26 #include "texstream.h"
27 #include "TextClass.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetArgument.h"
32 #include "frontends/alert.h"
34 #include "support/lassert.h"
35 #include "support/convert.h"
36 #include "support/debug.h"
37 #include "support/lstrings.h"
38 #include "support/lyxalgo.h"
39 #include "support/textutils.h"
40 #include "support/gettext.h"
42 #include <QThreadStorage>
48 using namespace lyx::support;
64 OutputState() : open_encoding_(none), cjk_inherited_(0),
65 prev_env_language_(0), nest_level_(0)
68 OpenEncoding open_encoding_;
70 Language const * prev_env_language_;
72 stack<int> lang_switch_depth_; // Both are always empty when
73 stack<string> open_polyglossia_lang_; // not using polyglossia
77 OutputState * getOutputState()
79 // FIXME An instance of OutputState should be kept around for each export
80 // instead of using local thread storage
81 static QThreadStorage<OutputState *> outputstate;
82 if (!outputstate.hasLocalData())
83 outputstate.setLocalData(new OutputState);
84 return outputstate.localData();
88 string const & openLanguageName(OutputState const * state)
90 // Return a reference to the last active language opened with
91 // polyglossia or when using begin/end commands. If none or when
92 // using babel with only a begin command, return a reference to
95 static string const empty;
97 return state->open_polyglossia_lang_.empty()
99 : state->open_polyglossia_lang_.top();
103 bool atSameLastLangSwitchDepth(OutputState const * state)
105 // Return true if the actual nest level is the same at which the
106 // language was switched when using polyglossia or begin/end
107 // commands. Instead, return always true when using babel with
108 // only a begin command.
110 return state->lang_switch_depth_.size() == 0
112 : abs(state->lang_switch_depth_.top()) == state->nest_level_;
116 bool isLocalSwitch(OutputState const * state)
118 // Return true if the language was opened by a local command switch.
120 return state->lang_switch_depth_.size()
121 && state->lang_switch_depth_.top() < 0;
125 bool langOpenedAtThisLevel(OutputState const * state)
127 // Return true if the language was opened at the current nesting level.
129 return state->lang_switch_depth_.size()
130 && abs(state->lang_switch_depth_.top()) == state->nest_level_;
134 string const getPolyglossiaEnvName(Language const * lang)
136 string result = lang->polyglossia();
137 if (result == "arabic")
138 // exceptional spelling; see polyglossia docs.
144 string const getPolyglossiaBegin(string const & lang_begin_command,
145 string const & lang, string const & opts,
146 bool const localswitch = false)
150 // we need to revert the upcasing done in getPolyglossiaEnvName()
151 // in case we have a local polyglossia command (\textarabic).
152 string language = localswitch ? ascii_lowercase(lang) : lang;
153 result = subst(lang_begin_command, "$$lang", language);
155 string options = opts.empty() ?
156 string() : "[" + opts + "]";
157 result = subst(result, "$$opts", options);
163 struct TeXEnvironmentData
166 Layout const * style;
167 Language const * par_language;
168 Encoding const * prev_encoding;
169 bool leftindent_open;
173 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
175 ParagraphList::const_iterator pit,
177 OutputParams const & runparams)
179 TeXEnvironmentData data;
181 BufferParams const & bparams = buf.params();
183 // FIXME This test should not be necessary.
184 // We should perhaps issue an error if it is.
185 Layout const & style = text.inset().forcePlainLayout() ?
186 bparams.documentClass().plainLayout() : pit->layout();
188 ParagraphList const & paragraphs = text.paragraphs();
189 ParagraphList::const_iterator const priorpit =
190 pit == paragraphs.begin() ? pit : prev(pit, 1);
192 OutputState * state = getOutputState();
193 bool const use_prev_env_language = state->prev_env_language_ != 0
194 && priorpit->layout().isEnvironment()
195 && (priorpit->getDepth() > pit->getDepth()
196 || (priorpit->getDepth() == pit->getDepth()
197 && priorpit->layout() != pit->layout()));
199 data.prev_encoding = runparams.encoding;
200 data.par_language = pit->getParLanguage(bparams);
201 Language const * const doc_language = bparams.language;
202 Language const * const prev_par_language =
203 (pit != paragraphs.begin())
204 ? (use_prev_env_language ? state->prev_env_language_
205 : priorpit->getParLanguage(bparams))
208 bool const use_polyglossia = runparams.use_polyglossia;
209 string const par_lang = use_polyglossia ?
210 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
211 string const prev_par_lang = use_polyglossia ?
212 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
213 string const doc_lang = use_polyglossia ?
214 getPolyglossiaEnvName(doc_language) : doc_language->babel();
215 string const lang_begin_command = use_polyglossia ?
216 "\\begin{$$lang}" : lyxrc.language_command_begin;
217 string const lang_end_command = use_polyglossia ?
218 "\\end{$$lang}" : lyxrc.language_command_end;
219 bool const using_begin_end = use_polyglossia ||
220 !lang_end_command.empty();
222 // For polyglossia, switch language outside of environment, if possible.
223 if (par_lang != prev_par_lang) {
224 if ((!using_begin_end || langOpenedAtThisLevel(state)) &&
225 !lang_end_command.empty() &&
226 prev_par_lang != doc_lang &&
227 !prev_par_lang.empty()) {
228 os << from_ascii(subst(
232 // the '%' is necessary to prevent unwanted whitespace
238 // If no language was explicitly opened and we are using
239 // polyglossia or begin/end commands, then the current
240 // language is the document language.
241 string const & cur_lang = using_begin_end
242 && state->lang_switch_depth_.size()
243 ? openLanguageName(state)
246 if ((lang_end_command.empty() ||
247 par_lang != doc_lang ||
248 par_lang != cur_lang) &&
250 string bc = use_polyglossia ?
251 getPolyglossiaBegin(lang_begin_command, par_lang,
252 data.par_language->polyglossiaOpts())
253 : subst(lang_begin_command, "$$lang", par_lang);
255 // the '%' is necessary to prevent unwanted whitespace
258 pushLanguageName(par_lang);
262 data.leftindent_open = false;
263 if (!pit->params().leftIndent().zero()) {
264 os << "\\begin{LyXParagraphLeftIndent}{"
265 << from_ascii(pit->params().leftIndent().asLatexString())
267 data.leftindent_open = true;
270 if (style.isEnvironment())
271 state->nest_level_ += 1;
273 if (style.isEnvironment() && !style.latexname().empty()) {
274 os << "\\begin{" << from_ascii(style.latexname()) << '}';
275 if (!style.latexargs().empty()) {
276 OutputParams rp = runparams;
277 rp.local_font = &pit->getFirstFontSettings(bparams);
278 latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
280 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
282 << pit->params().labelWidthString()
284 } else if (style.labeltype == LABEL_BIBLIO) {
285 if (pit->params().labelWidthString().empty())
286 os << '{' << bibitemWidest(buf, runparams) << "}\n";
289 << pit->params().labelWidthString()
292 os << from_ascii(style.latexparam()) << '\n';
293 if (style.latextype == LATEX_BIB_ENVIRONMENT
294 || style.latextype == LATEX_ITEM_ENVIRONMENT
295 || style.latextype == LATEX_LIST_ENVIRONMENT) {
296 OutputParams rp = runparams;
297 rp.local_font = &pit->getFirstFontSettings(bparams);
298 latexArgInsets(paragraphs, pit, os, rp, style.listpreamble(),
304 // in multilingual environments, the CJK tags have to be nested properly
305 data.cjk_nested = false;
306 if (data.par_language->encoding()->package() == Encoding::CJK &&
307 state->open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
308 if (prev_par_language->encoding()->package() == Encoding::CJK)
309 os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
310 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
311 state->open_encoding_ = CJK;
312 data.cjk_nested = true;
318 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
319 TeXEnvironmentData const & data)
321 OutputState * state = getOutputState();
322 // BufferParams const & bparams = buf.params(); // FIXME: for speedup shortcut below, would require passing of "buf" as argument
323 if (state->open_encoding_ == CJK && data.cjk_nested) {
324 // We need to close the encoding even if it does not change
325 // to do correct environment nesting
326 os << "\\end{CJK}\n";
327 state->open_encoding_ = none;
330 if (data.style->isEnvironment()) {
332 bool const using_begin_end =
333 runparams.use_polyglossia ||
334 !lyxrc.language_command_end.empty();
335 // Close any language opened at this nest level
336 if (using_begin_end) {
337 while (langOpenedAtThisLevel(state)) {
338 if (isLocalSwitch(state)) {
342 << openLanguageName(state)
348 state->nest_level_ -= 1;
349 string const & name = data.style->latexname();
351 os << "\\end{" << from_ascii(name) << "}\n";
352 state->prev_env_language_ = data.par_language;
353 if (runparams.encoding != data.prev_encoding) {
354 runparams.encoding = data.prev_encoding;
355 os << setEncoding(data.prev_encoding->iconvName());
359 if (data.leftindent_open) {
360 os << breakln << "\\end{LyXParagraphLeftIndent}\n";
361 state->prev_env_language_ = data.par_language;
362 if (runparams.encoding != data.prev_encoding) {
363 runparams.encoding = data.prev_encoding;
364 os << setEncoding(data.prev_encoding->iconvName());
368 // Check whether we should output a blank line after the environment
369 if (!data.style->nextnoindent)
374 void TeXEnvironment(Buffer const & buf, Text const & text,
375 OutputParams const & runparams,
376 pit_type & pit, otexstream & os)
378 ParagraphList const & paragraphs = text.paragraphs();
379 ParagraphList::const_iterator ipar = paragraphs.constIterator(pit);
380 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
382 Layout const & current_layout = ipar->layout();
383 depth_type const current_depth = ipar->params().depth();
384 Length const & current_left_indent = ipar->params().leftIndent();
386 // This is for debugging purpose at the end.
387 pit_type const par_begin = pit;
388 for (; pit < runparams.par_end; ++pit) {
389 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
391 // check first if this is an higher depth paragraph.
392 bool go_out = (par->params().depth() < current_depth);
393 if (par->params().depth() == current_depth) {
394 // This environment is finished.
395 go_out |= (par->layout() != current_layout);
396 go_out |= (par->params().leftIndent() != current_left_indent);
399 // nothing to do here, restore pit and go out.
404 if (par->layout() == current_layout
405 && par->params().depth() == current_depth
406 && par->params().leftIndent() == current_left_indent) {
407 // We are still in the same environment so TeXOnePar and continue;
408 TeXOnePar(buf, text, pit, os, runparams);
412 // We are now in a deeper environment.
413 // Either par->layout() != current_layout
414 // Or par->params().depth() > current_depth
415 // Or par->params().leftIndent() != current_left_indent)
417 // FIXME This test should not be necessary.
418 // We should perhaps issue an error if it is.
419 bool const force_plain_layout = text.inset().forcePlainLayout();
420 Layout const & style = force_plain_layout
421 ? buf.params().documentClass().plainLayout()
424 if (!style.isEnvironment()) {
425 // This is a standard paragraph, no need to call TeXEnvironment.
426 TeXOnePar(buf, text, pit, os, runparams);
430 // This is a new environment.
431 TeXEnvironmentData const data =
432 prepareEnvironment(buf, text, par, os, runparams);
433 // Recursive call to TeXEnvironment!
434 TeXEnvironment(buf, text, runparams, pit, os);
435 finishEnvironment(os, runparams, data);
438 if (pit != runparams.par_end)
439 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
443 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
444 map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
446 unsigned int const argnr = latexargs.size();
450 // Default and preset args are always output, so if they require
451 // other arguments, consider this.
452 for (auto const & larg : latexargs) {
453 Layout::latexarg const & arg = larg.second;
454 if ((!arg.presetarg.empty() || !arg.defaultarg.empty()) && !arg.requires.empty()) {
455 vector<string> req = getVectorFromString(arg.requires);
456 required.insert(required.end(), req.begin(), req.end());
460 for (unsigned int i = 1; i <= argnr; ++i) {
461 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
462 bool inserted = false;
463 if (lit != ilist.end()) {
464 InsetArgument const * ins = (*lit).second;
466 Layout::LaTeXArgMap::const_iterator const lait =
467 latexargs.find(ins->name());
468 if (lait != latexargs.end()) {
469 Layout::latexarg arg = (*lait).second;
473 ldelim = arg.mandatory ?
474 from_ascii("{") : from_ascii("[");
475 rdelim = arg.mandatory ?
476 from_ascii("}") : from_ascii("]");
478 if (!arg.ldelim.empty())
480 if (!arg.rdelim.empty())
482 ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
483 if (prefix == "listpreamble:")
490 Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
491 Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
492 for (; lait != laend; ++lait) {
493 string const name = prefix + convert<string>(i);
494 if ((*lait).first == name) {
495 Layout::latexarg arg = (*lait).second;
496 docstring preset = arg.presetarg;
497 if (!arg.defaultarg.empty()) {
500 preset += arg.defaultarg;
503 docstring ldelim = arg.ldelim.empty() ?
504 from_ascii("{") : arg.ldelim;
505 docstring rdelim = arg.rdelim.empty() ?
506 from_ascii("}") : arg.rdelim;
507 os << ldelim << preset << rdelim;
508 } else if (!preset.empty()) {
509 docstring ldelim = arg.ldelim.empty() ?
510 from_ascii("[") : arg.ldelim;
511 docstring rdelim = arg.rdelim.empty() ?
512 from_ascii("]") : arg.rdelim;
513 os << ldelim << preset << rdelim;
514 } else if (find(required.begin(), required.end(),
515 (*lait).first) != required.end()) {
516 docstring ldelim = arg.ldelim.empty() ?
517 from_ascii("[") : arg.ldelim;
518 docstring rdelim = arg.rdelim.empty() ?
519 from_ascii("]") : arg.rdelim;
520 os << ldelim << rdelim;
533 void pushLanguageName(string const & lang_name, bool localswitch)
535 OutputState * state = getOutputState();
537 int nest_level = localswitch ? -state->nest_level_ : state->nest_level_;
538 state->lang_switch_depth_.push(nest_level);
539 state->open_polyglossia_lang_.push(lang_name);
543 void popLanguageName()
545 OutputState * state = getOutputState();
547 state->lang_switch_depth_.pop();
548 state->open_polyglossia_lang_.pop();
552 string const & openLanguageName()
554 OutputState * state = getOutputState();
556 return openLanguageName(state);
562 void addArgInsets(Paragraph const & par, string const & prefix,
563 Layout::LaTeXArgMap const & latexargs,
564 map<int, InsetArgument const *> & ilist,
565 vector<string> & required)
567 for (auto const & table : par.insetList()) {
568 InsetArgument const * arg = table.inset->asInsetArgument();
571 if (arg->name().empty()) {
572 LYXERR0("Error: Unnamed argument inset!");
575 string const name = prefix.empty() ?
576 arg->name() : split(arg->name(), ':');
577 // why converting into an integer?
578 unsigned int const nr = convert<unsigned int>(name);
579 if (ilist.find(nr) == ilist.end())
581 Layout::LaTeXArgMap::const_iterator const lit =
582 latexargs.find(arg->name());
583 if (lit != latexargs.end()) {
584 Layout::latexarg const & larg = lit->second;
585 vector<string> req = getVectorFromString(larg.requires);
586 move(req.begin(), req.end(), back_inserter(required));
594 void latexArgInsets(Paragraph const & par, otexstream & os,
595 OutputParams const & runparams,
596 Layout::LaTeXArgMap const & latexargs,
597 string const & prefix)
599 map<int, InsetArgument const *> ilist;
600 vector<string> required;
601 addArgInsets(par, prefix, latexargs, ilist, required);
602 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
606 void latexArgInsets(ParagraphList const & pars,
607 ParagraphList::const_iterator pit,
608 otexstream & os, OutputParams const & runparams,
609 Layout::LaTeXArgMap const & latexargs,
610 string const & prefix)
612 map<int, InsetArgument const *> ilist;
613 vector<string> required;
615 depth_type const current_depth = pit->params().depth();
616 Layout const current_layout = pit->layout();
618 // get the first paragraph in sequence with this layout and depth
621 if (lyx::prev(pit, offset) == pars.begin())
623 ParagraphList::const_iterator priorpit = lyx::prev(pit, offset + 1);
624 if (priorpit->layout() == current_layout
625 && priorpit->params().depth() == current_depth)
631 ParagraphList::const_iterator spit = lyx::prev(pit, offset);
633 for (; spit != pars.end(); ++spit) {
634 if (spit->layout() != current_layout ||
635 spit->params().depth() < current_depth)
637 if (spit->params().depth() > current_depth)
639 addArgInsets(*spit, prefix, latexargs, ilist, required);
641 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
645 void latexArgInsetsForParent(ParagraphList const & pars, otexstream & os,
646 OutputParams const & runparams,
647 Layout::LaTeXArgMap const & latexargs,
648 string const & prefix)
650 map<int, InsetArgument const *> ilist;
651 vector<string> required;
653 for (Paragraph const & par : pars) {
654 if (par.layout().hasArgs())
655 // The InsetArguments inside this paragraph refer to this paragraph
657 addArgInsets(par, prefix, latexargs, ilist, required);
659 getArgInsets(os, runparams, latexargs, ilist, required, prefix);
665 // output the proper paragraph start according to latextype.
666 void parStartCommand(Paragraph const & par, otexstream & os,
667 OutputParams const & runparams, Layout const & style)
669 switch (style.latextype) {
671 if (par.needsCProtection(runparams.moving_arg))
673 os << '\\' << from_ascii(style.latexname());
676 if (!style.latexargs().empty())
677 latexArgInsets(par, os, runparams, style.latexargs());
678 os << from_ascii(style.latexparam());
680 case LATEX_ITEM_ENVIRONMENT:
681 case LATEX_LIST_ENVIRONMENT:
682 os << "\\" + style.itemcommand();
684 if (!style.itemargs().empty())
685 latexArgInsets(par, os, runparams, style.itemargs(), "item:");
688 case LATEX_BIB_ENVIRONMENT:
689 // ignore this, the inset will write itself
698 // FIXME: this should be anonymous
699 void TeXOnePar(Buffer const & buf,
703 OutputParams const & runparams_in,
704 string const & everypar,
705 int start_pos, int end_pos,
708 BufferParams const & bparams = runparams_in.is_child
709 ? buf.masterParams() : buf.params();
710 ParagraphList const & paragraphs = text.paragraphs();
711 Paragraph const & par = paragraphs.at(pit);
712 // FIXME This check should not really be needed.
713 // Perhaps we should issue an error if it is.
714 Layout const & style = text.inset().forcePlainLayout() ?
715 bparams.documentClass().plainLayout() : par.layout();
717 if (style.inpreamble && !force)
720 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
723 OutputParams runparams = runparams_in;
724 runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
725 // We reinitialze par begin and end to be on the safe side
726 // with embedded inset as we don't know if they set those
728 runparams.par_begin = 0;
729 runparams.par_end = 0;
731 bool const maintext = text.isMainText();
732 // we are at the beginning of an inset and CJK is already open;
733 // we count inheritation levels to get the inset nesting right.
734 OutputState * state = getOutputState();
735 if (pit == 0 && !maintext
736 && (state->cjk_inherited_ > 0 || state->open_encoding_ == CJK)) {
737 state->cjk_inherited_ += 1;
738 state->open_encoding_ = none;
741 if (text.inset().isPassThru()) {
742 Font const outerfont = text.outerFont(pit);
744 // No newline before first paragraph in this lyxtext
747 if (!text.inset().getLayout().parbreakIsNewline())
751 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
755 Paragraph const * nextpar = runparams.isLastPar
756 ? 0 : ¶graphs.at(pit + 1);
758 bool const intitle_command = style.intitle && style.latextype == LATEX_COMMAND;
760 if (style.pass_thru) {
761 Font const outerfont = text.outerFont(pit);
762 parStartCommand(par, os, runparams, style);
766 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
768 // I did not create a parEndCommand for this minuscule
769 // task because in the other user of parStartCommand
770 // the code is different (JMarc)
771 if (style.isCommand())
775 if (!style.parbreak_is_newline) {
777 } else if (nextpar && !style.isEnvironment()) {
778 Layout const nextstyle = text.inset().forcePlainLayout()
779 ? bparams.documentClass().plainLayout()
781 if (nextstyle.name() != style.name())
788 // This paragraph's language
789 Language const * const par_language = par.getParLanguage(bparams);
790 Language const * const nextpar_language = nextpar ?
791 nextpar->getParLanguage(bparams) : 0;
792 // The document's language
793 Language const * const doc_language = bparams.language;
794 // The language that was in effect when the environment this paragraph is
795 // inside of was opened
796 Language const * const outer_language =
797 (runparams.local_font != 0) ?
798 runparams.local_font->language() : doc_language;
800 Paragraph const * priorpar = (pit == 0) ? 0 : ¶graphs.at(pit - 1);
802 // The previous language that was in effect is the language of the
803 // previous paragraph, unless the previous paragraph is inside an
804 // environment with nesting depth greater than (or equal to, but with
805 // a different layout) the current one. If there is no previous
806 // paragraph, the previous language is the outer language.
807 // Note further that we take the outer language also if the prior par
808 // is PassThru, since in that case it has latex_language, and all secondary
809 // languages have been closed (#10793).
810 bool const use_prev_env_language = state->prev_env_language_ != 0
812 && priorpar->layout().isEnvironment()
813 && (priorpar->getDepth() > par.getDepth()
814 || (priorpar->getDepth() == par.getDepth()
815 && priorpar->layout() != par.layout()));
816 Language const * const prev_language =
817 (priorpar && !priorpar->isPassThru())
818 ? (use_prev_env_language ? state->prev_env_language_
819 : priorpar->getParLanguage(bparams))
822 bool const use_polyglossia = runparams.use_polyglossia;
823 string const par_lang = use_polyglossia ?
824 getPolyglossiaEnvName(par_language): par_language->babel();
825 string const prev_lang = use_polyglossia ?
826 getPolyglossiaEnvName(prev_language) : prev_language->babel();
827 string const outer_lang = use_polyglossia ?
828 getPolyglossiaEnvName(outer_language) : outer_language->babel();
829 string const nextpar_lang = nextpar_language ? (use_polyglossia ?
830 getPolyglossiaEnvName(nextpar_language) :
831 nextpar_language->babel()) : string();
832 string lang_begin_command = use_polyglossia ?
833 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
834 string lang_end_command = use_polyglossia ?
835 "\\end{$$lang}" : lyxrc.language_command_end;
836 // the '%' is necessary to prevent unwanted whitespace
837 string lang_command_termination = "%\n";
838 bool const using_begin_end = use_polyglossia ||
839 !lang_end_command.empty();
841 // For InTitle commands, we need to switch the language inside the command
842 // (see #10849); thus open the command here.
843 if (intitle_command) {
844 parStartCommand(par, os, runparams, style);
848 // In some insets (such as Arguments), we cannot use \selectlanguage
849 bool const localswitch = text.inset().forceLocalFontSwitch()
850 || (using_begin_end && text.inset().forcePlainLayout());
852 lang_begin_command = use_polyglossia ?
853 "\\text$$lang$$opts{" : lyxrc.language_command_local;
854 lang_end_command = "}";
855 lang_command_termination.clear();
858 bool const localswitch_needed = localswitch && par_lang != outer_lang;
860 // localswitches need to be closed and reopened at each par
861 if ((par_lang != prev_lang || localswitch_needed)
862 // check if we already put language command in TeXEnvironment()
863 && !(style.isEnvironment()
864 && (pit == 0 || (priorpar->layout() != par.layout()
865 && priorpar->getDepth() <= par.getDepth())
866 || priorpar->getDepth() < par.getDepth()))) {
868 && (!using_begin_end || langOpenedAtThisLevel(state))
869 && !lang_end_command.empty()
870 && prev_lang != outer_lang
871 && !prev_lang.empty()
872 && (!using_begin_end || !style.isEnvironment())) {
873 os << from_ascii(subst(lang_end_command,
876 << lang_command_termination;
881 // We need to open a new language if we couldn't close the previous
882 // one (because there's no language_command_end); and even if we closed
883 // the previous one, if the current language is different than the
884 // outer_language (which is currently in effect once the previous one
886 if ((lang_end_command.empty() || par_lang != outer_lang
888 || (style.isEnvironment() && par_lang != prev_lang)))
889 && !par_lang.empty()) {
890 // If we're inside an inset, and that inset is within an \L or \R
891 // (or equivalents), then within the inset, too, any opposite
892 // language paragraph should appear within an \L or \R (in addition
893 // to, outside of, the normal language switch commands).
894 // This behavior is not correct for ArabTeX, though.
897 && par_language->lang() != "arabic_arabtex"
898 && outer_language->lang() != "arabic_arabtex"
899 // are we in an inset?
900 && runparams.local_font != 0
901 // is the inset within an \L or \R?
903 // FIXME: currently, we don't check this; this means that
904 // we'll have unnnecessary \L and \R commands, but that
905 // doesn't seem to hurt (though latex will complain)
907 // is this paragraph in the opposite direction?
908 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
909 // FIXME: I don't have a working copy of the Arabi package, so
910 // I'm not sure if the farsi and arabic_arabi stuff is correct
912 if (par_language->lang() == "farsi")
914 else if (outer_language->lang() == "farsi")
916 else if (par_language->lang() == "arabic_arabi")
918 else if (outer_language->lang() == "arabic_arabi")
920 // remaining RTL languages currently is hebrew
921 else if (par_language->rightToLeft())
926 // With CJK, the CJK tag has to be closed first (see below)
927 if (runparams.encoding->package() != Encoding::CJK
928 && (par_lang != openLanguageName(state) || localswitch)
929 && !par_lang.empty()) {
930 string bc = use_polyglossia ?
931 getPolyglossiaBegin(lang_begin_command, par_lang,
932 par_language->polyglossiaOpts(),
934 : subst(lang_begin_command, "$$lang", par_lang);
936 os << lang_command_termination;
938 pushLanguageName(par_lang, localswitch);
943 // Switch file encoding if necessary; no need to do this for "default"
944 // encoding, since this only affects the position of the outputted
945 // \inputencoding command; the encoding switch will occur when necessary
946 if (bparams.inputenc == "auto"
947 && !runparams.isFullUnicode() // Xe/LuaTeX use one document-wide encoding (see also switchEncoding())
948 && runparams.encoding->package() != Encoding::none) {
949 // Look ahead for future encoding changes.
950 // We try to output them at the beginning of the paragraph,
951 // since the \inputencoding command is not allowed e.g. in
952 // sections. For this reason we only set runparams.moving_arg
953 // after checking for the encoding change, otherwise the
954 // change would be always avoided by switchEncoding().
955 for (pos_type i = 0; i < par.size(); ++i) {
956 char_type const c = par.getChar(i);
957 Encoding const * const encoding =
958 par.getFontSettings(bparams, i).language()->encoding();
959 if (encoding->package() != Encoding::CJK
960 && runparams.encoding->package() == Encoding::inputenc
965 // All characters before c are in the ASCII range, and
966 // c is non-ASCII (but no inset), so change the
967 // encoding to that required by the language of c.
968 // With CJK, only add switch if we have CJK content at the beginning
970 if (i != 0 && encoding->package() == Encoding::CJK)
973 pair<bool, int> enc_switch = switchEncoding(os.os(),
974 bparams, runparams, *encoding);
975 // the following is necessary after a CJK environment in a multilingual
976 // context (nesting issue).
977 if (par_language->encoding()->package() == Encoding::CJK
978 && state->open_encoding_ != CJK && state->cjk_inherited_ == 0) {
979 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
980 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
981 state->open_encoding_ = CJK;
983 if (encoding->package() != Encoding::none && enc_switch.first) {
984 if (enc_switch.second > 0) {
985 // the '%' is necessary to prevent unwanted whitespace
988 // With CJK, the CJK tag had to be closed first (see above)
989 if (runparams.encoding->package() == Encoding::CJK
990 && par_lang != openLanguageName(state)
991 && !par_lang.empty()) {
992 string bc = use_polyglossia ?
993 getPolyglossiaBegin(lang_begin_command, par_lang,
994 par_language->polyglossiaOpts(),
996 : subst(lang_begin_command, "$$lang", par_lang);
998 << lang_command_termination;
1000 pushLanguageName(par_lang, localswitch);
1002 runparams.encoding = encoding;
1008 runparams.moving_arg |= style.needprotect;
1009 Encoding const * const prev_encoding = runparams.encoding;
1011 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
1012 if (par.allowParagraphCustomization()) {
1013 if (par.params().startOfAppendix()) {
1014 os << "\n\\appendix\n";
1017 // InTitle commands must use switches (not environments)
1018 // inside the commands (see #9332)
1019 if (style.intitle) {
1020 if (!par.params().spacing().isDefault())
1022 if (runparams.moving_arg)
1024 os << from_ascii(par.params().spacing().writeCmd(useSetSpace));
1027 if (!par.params().spacing().isDefault()
1028 && (pit == 0 || !priorpar->hasSameLayout(par)))
1030 os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
1034 if (style.isCommand()) {
1040 // For InTitle commands, we already started the command before
1041 // the language switch
1042 if (!intitle_command)
1043 parStartCommand(par, os, runparams, style);
1045 Font const outerfont = text.outerFont(pit);
1048 os << from_utf8(everypar);
1049 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos, force);
1051 Font const font = par.empty()
1052 ? par.getLayoutFont(bparams, outerfont)
1053 : par.getFont(bparams, par.size() - 1, outerfont);
1055 bool const is_command = style.isCommand();
1057 // InTitle commands need to be closed after the language has been closed.
1058 if (!intitle_command) {
1061 if (!style.postcommandargs().empty())
1062 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
1063 if (runparams.encoding != prev_encoding) {
1064 runparams.encoding = prev_encoding;
1065 os << setEncoding(prev_encoding->iconvName());
1070 bool pending_newline = false;
1071 bool unskip_newline = false;
1072 bool close_lang_switch = false;
1073 switch (style.latextype) {
1074 case LATEX_ITEM_ENVIRONMENT:
1075 case LATEX_LIST_ENVIRONMENT:
1076 if ((nextpar && par_lang != nextpar_lang
1077 && nextpar->getDepth() == par.getDepth())
1078 || (atSameLastLangSwitchDepth(state) && nextpar
1079 && nextpar->getDepth() < par.getDepth()))
1080 close_lang_switch = using_begin_end;
1081 if (nextpar && par.params().depth() < nextpar->params().depth())
1082 pending_newline = true;
1084 case LATEX_ENVIRONMENT: {
1085 // if its the last paragraph of the current environment
1086 // skip it otherwise fall through
1088 && ((nextpar->layout() != par.layout()
1089 || nextpar->params().depth() != par.params().depth())
1090 || (!using_begin_end || par_lang != nextpar_lang)))
1092 close_lang_switch = using_begin_end;
1099 // we don't need it for the last paragraph and in InTitle commands!!!
1100 if (nextpar && !intitle_command)
1101 pending_newline = true;
1104 // InTitle commands use switches (not environments) for space settings
1105 if (par.allowParagraphCustomization() && !style.intitle) {
1106 if (!par.params().spacing().isDefault()
1107 && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
1108 if (pending_newline)
1111 string const endtag =
1112 par.params().spacing().writeEnvirEnd(useSetSpace);
1113 if (prefixIs(endtag, "\\end{"))
1116 os << from_ascii(endtag);
1117 pending_newline = true;
1121 // Closing the language is needed for the last paragraph in a given language
1122 // as well as for any InTitleCommand (since these set the language locally);
1123 // it is also needed if we're within an \L or \R that we may have opened above
1124 // (not necessarily in this paragraph) and are about to close.
1125 bool closing_rtl_ltr_environment = !using_begin_end
1127 && (par_language->lang() != "arabic_arabtex"
1128 && outer_language->lang() != "arabic_arabtex")
1129 // have we opened an \L or \R environment?
1130 && runparams.local_font != 0
1131 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
1132 // are we about to close the language?
1133 &&((nextpar && par_lang != nextpar_lang)
1134 || (runparams.isLastPar && par_lang != outer_lang));
1136 if (localswitch_needed
1137 || (intitle_command && using_begin_end)
1138 || closing_rtl_ltr_environment
1139 || ((runparams.isLastPar || close_lang_switch)
1140 && (par_lang != outer_lang || (using_begin_end
1141 && style.isEnvironment()
1142 && par_lang != nextpar_lang)))) {
1143 // Since \selectlanguage write the language to the aux file,
1144 // we need to reset the language at the end of footnote or
1147 if (!localswitch && (pending_newline || close_lang_switch))
1150 // when the paragraph uses CJK, the language has to be closed earlier
1151 if (font.language()->encoding()->package() != Encoding::CJK) {
1152 if (lang_end_command.empty()) {
1153 // If this is a child, we should restore the
1154 // master language after the last paragraph.
1155 Language const * const current_language =
1156 (runparams.isLastPar && runparams.master_language)
1157 ? runparams.master_language
1159 string const current_lang = use_polyglossia
1160 ? getPolyglossiaEnvName(current_language)
1161 : current_language->babel();
1162 if (!current_lang.empty()
1163 && current_lang != openLanguageName(state)) {
1164 string bc = use_polyglossia ?
1165 getPolyglossiaBegin(lang_begin_command, current_lang,
1166 current_language->polyglossiaOpts(),
1168 : subst(lang_begin_command, "$$lang", current_lang);
1170 pending_newline = !localswitch;
1171 unskip_newline = !localswitch;
1172 if (using_begin_end)
1173 pushLanguageName(current_lang, localswitch);
1175 } else if ((!using_begin_end ||
1176 langOpenedAtThisLevel(state)) &&
1177 !par_lang.empty()) {
1178 // If we are in an environment, we have to
1179 // close the "outer" language afterwards
1180 string const & cur_lang = openLanguageName(state);
1181 if (!style.isEnvironment()
1182 || (close_lang_switch
1183 && atSameLastLangSwitchDepth(state)
1184 && par_lang != outer_lang
1185 && (par_lang != cur_lang
1186 || (cur_lang != outer_lang
1188 && style != nextpar->layout())))
1189 || (atSameLastLangSwitchDepth(state)
1190 && state->lang_switch_depth_.size()
1191 && cur_lang != par_lang))
1193 if (using_begin_end && !localswitch)
1195 os << from_ascii(subst(
1199 pending_newline = !localswitch;
1200 unskip_newline = !localswitch;
1201 if (using_begin_end)
1207 if (closing_rtl_ltr_environment)
1210 // InTitle commands need to be closed after the language has been closed.
1211 if (intitle_command) {
1214 if (!style.postcommandargs().empty())
1215 latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
1216 if (runparams.encoding != prev_encoding) {
1217 runparams.encoding = prev_encoding;
1218 os << setEncoding(prev_encoding->iconvName());
1223 bool const last_was_separator =
1224 par.size() > 0 && par.isEnvSeparator(par.size() - 1);
1226 if (pending_newline) {
1228 // prevent unwanted whitespace
1230 if (!os.afterParbreak() && !last_was_separator)
1234 // if this is a CJK-paragraph and the next isn't, close CJK
1235 // also if the next paragraph is a multilingual environment (because of nesting)
1237 && state->open_encoding_ == CJK
1238 && (nextpar_language->encoding()->package() != Encoding::CJK
1239 || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
1240 // inbetween environments, CJK has to be closed later (nesting!)
1241 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
1242 os << "\\end{CJK}\n";
1243 state->open_encoding_ = none;
1246 // If this is the last paragraph, close the CJK environment
1247 // if necessary. If it's an environment, we'll have to \end that first.
1248 if (runparams.isLastPar && !style.isEnvironment()) {
1249 switch (state->open_encoding_) {
1251 // do nothing at the end of child documents
1252 if (maintext && buf.masterBuffer() != &buf)
1256 os << "\n\\end{CJK}\n";
1260 state->open_encoding_ = none;
1265 state->open_encoding_ = none;
1275 // If this is the last paragraph, and a local_font was set upon entering
1276 // the inset, and we're using "auto" or "default" encoding, and not
1277 // compiling with XeTeX or LuaTeX, the encoding
1278 // should be set back to that local_font's encoding.
1279 if (runparams.isLastPar && runparams_in.local_font != 0
1280 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
1281 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
1282 && !runparams.isFullUnicode()
1284 runparams_in.encoding = runparams_in.local_font->language()->encoding();
1285 os << setEncoding(runparams_in.encoding->iconvName());
1287 // Otherwise, the current encoding should be set for the next paragraph.
1289 runparams_in.encoding = runparams.encoding;
1292 // we don't need a newline for the last paragraph!!!
1293 // Note from JMarc: we will re-add a \n explicitly in
1294 // TeXEnvironment, because it is needed in this case
1295 if (nextpar && !os.afterParbreak() && !last_was_separator) {
1296 // Make sure to start a new line
1298 Layout const & next_layout = nextpar->layout();
1299 // A newline '\n' is always output before a command,
1300 // so avoid doubling it.
1301 if (!next_layout.isCommand()) {
1302 // Here we now try to avoid spurious empty lines by
1303 // outputting a paragraph break only if: (case 1) the
1304 // paragraph style allows parbreaks and no \begin, \end
1305 // or \item tags are going to follow (i.e., if the next
1306 // isn't the first or the current isn't the last
1307 // paragraph of an environment or itemize) and the
1308 // depth and alignment of the following paragraph is
1309 // unchanged, or (case 2) the following is a
1310 // non-environment paragraph whose depth is increased
1311 // but whose alignment is unchanged, or (case 3) the
1312 // paragraph is not an environment and the next one is a
1313 // non-itemize-like env at lower depth, or (case 4) the
1314 // paragraph is a command not followed by an environment
1315 // and the alignment of the current and next paragraph
1316 // is unchanged, or (case 5) the current alignment is
1317 // changed and a standard paragraph follows.
1318 DocumentClass const & tclass = bparams.documentClass();
1319 if ((style == next_layout
1320 && !style.parbreak_is_newline
1321 && !text.inset().getLayout().parbreakIsNewline()
1322 && style.latextype != LATEX_ITEM_ENVIRONMENT
1323 && style.latextype != LATEX_LIST_ENVIRONMENT
1324 && style.align == par.getAlign()
1325 && nextpar->getDepth() == par.getDepth()
1326 && nextpar->getAlign() == par.getAlign())
1327 || (!next_layout.isEnvironment()
1328 && nextpar->getDepth() > par.getDepth()
1329 && nextpar->getAlign() == par.getAlign())
1330 || (!style.isEnvironment()
1331 && next_layout.latextype == LATEX_ENVIRONMENT
1332 && nextpar->getDepth() < par.getDepth())
1333 || (style.isCommand()
1334 && !next_layout.isEnvironment()
1335 && style.align == par.getAlign()
1336 && next_layout.align == nextpar->getAlign())
1337 || (style.align != par.getAlign()
1338 && tclass.isDefaultLayout(next_layout))) {
1344 LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1345 << &par << " next " << nextpar);
1351 // LaTeX all paragraphs
1352 void latexParagraphs(Buffer const & buf,
1355 OutputParams const & runparams,
1356 string const & everypar)
1358 LASSERT(runparams.par_begin <= runparams.par_end,
1359 { os << "% LaTeX Output Error\n"; return; } );
1361 BufferParams const & bparams = buf.params();
1362 BufferParams const & mparams = buf.masterParams();
1364 bool const maintext = text.isMainText();
1365 bool const is_child = buf.masterBuffer() != &buf;
1366 bool const multibib_child = maintext && is_child
1367 && mparams.multibib == "child";
1369 if (multibib_child && mparams.useBiblatex())
1370 os << "\\newrefsection";
1371 else if (multibib_child && mparams.useBibtopic()) {
1372 os << "\\begin{btUnit}\n";
1373 runparams.openbtUnit = true;
1376 // Open a CJK environment at the beginning of the main buffer
1377 // if the document's language is a CJK language
1378 // (but not in child documents)
1379 OutputState * state = getOutputState();
1380 if (maintext && !is_child
1381 && bparams.encoding().package() == Encoding::CJK) {
1382 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1383 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1384 state->open_encoding_ = CJK;
1386 // if "auto begin" is switched off, explicitly switch the
1387 // language on at start
1388 string const mainlang = runparams.use_polyglossia
1389 ? getPolyglossiaEnvName(bparams.language)
1390 : bparams.language->babel();
1391 string const lang_begin_command = runparams.use_polyglossia ?
1392 "\\begin{$$lang}$$opts" : lyxrc.language_command_begin;
1393 string const lang_end_command = runparams.use_polyglossia ?
1394 "\\end{$$lang}" : lyxrc.language_command_end;
1395 bool const using_begin_end = runparams.use_polyglossia ||
1396 !lang_end_command.empty();
1398 if (maintext && !lyxrc.language_auto_begin &&
1399 !mainlang.empty()) {
1401 string bc = runparams.use_polyglossia ?
1402 getPolyglossiaBegin(lang_begin_command, mainlang,
1403 bparams.language->polyglossiaOpts())
1404 : subst(lang_begin_command, "$$lang", mainlang);
1407 if (using_begin_end)
1408 pushLanguageName(mainlang);
1411 ParagraphList const & paragraphs = text.paragraphs();
1413 if (runparams.par_begin == runparams.par_end) {
1414 // The full doc will be exported but it is easier to just rely on
1415 // runparams range parameters that will be passed TeXEnvironment.
1416 runparams.par_begin = 0;
1417 runparams.par_end = paragraphs.size();
1420 pit_type pit = runparams.par_begin;
1421 // lastpit is for the language check after the loop.
1422 pit_type lastpit = pit;
1423 // variables used in the loop:
1424 bool was_title = false;
1425 bool already_title = false;
1426 DocumentClass const & tclass = bparams.documentClass();
1428 // Did we already warn about inTitle layout mixing? (we only warn once)
1429 bool gave_layout_warning = false;
1430 for (; pit < runparams.par_end; ++pit) {
1432 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1434 // FIXME This check should not be needed. We should
1435 // perhaps issue an error if it is.
1436 Layout const & layout = text.inset().forcePlainLayout() ?
1437 tclass.plainLayout() : par->layout();
1439 if (layout.intitle) {
1440 if (already_title) {
1441 if (!gave_layout_warning && !runparams.dryrun) {
1442 gave_layout_warning = true;
1443 frontend::Alert::warning(_("Error in latexParagraphs"),
1444 bformat(_("You are using at least one "
1445 "layout (%1$s) intended for the title, "
1446 "after using non-title layouts. This "
1447 "could lead to missing or incorrect output."
1450 } else if (!was_title) {
1452 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1454 << from_ascii(tclass.titlename())
1458 } else if (was_title && !already_title && !layout.inpreamble) {
1459 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1460 os << "\\end{" << from_ascii(tclass.titlename())
1464 os << "\\" << from_ascii(tclass.titlename())
1467 already_title = true;
1471 if (layout.isCommand() && !layout.latexname().empty()
1472 && layout.latexname() == bparams.multibib) {
1473 if (runparams.openbtUnit)
1474 os << "\\end{btUnit}\n";
1475 if (!bparams.useBiblatex()) {
1476 os << '\n' << "\\begin{btUnit}\n";
1477 runparams.openbtUnit = true;
1481 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1482 // This is a standard top level paragraph, TeX it and continue.
1483 TeXOnePar(buf, text, pit, os, runparams, everypar);
1487 TeXEnvironmentData const data =
1488 prepareEnvironment(buf, text, par, os, runparams);
1489 // pit can be changed in TeXEnvironment.
1490 TeXEnvironment(buf, text, runparams, pit, os);
1491 finishEnvironment(os, runparams, data);
1494 if (pit == runparams.par_end) {
1495 // Make sure that the last paragraph is
1496 // correctly terminated (because TeXOnePar does
1497 // not add a \n in this case)
1501 // It might be that we only have a title in this document
1502 if (was_title && !already_title) {
1503 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1504 os << "\\end{" << from_ascii(tclass.titlename())
1507 os << "\\" << from_ascii(tclass.titlename())
1512 if (maintext && !is_child && runparams.openbtUnit)
1513 os << "\\end{btUnit}\n";
1515 // if "auto end" is switched off, explicitly close the language at the end
1516 // but only if the last par is in a babel or polyglossia language
1517 if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1518 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1519 os << from_utf8(subst(lang_end_command,
1523 if (using_begin_end)
1527 // If the last paragraph is an environment, we'll have to close
1528 // CJK at the very end to do proper nesting.
1529 if (maintext && !is_child && state->open_encoding_ == CJK) {
1530 os << "\\end{CJK}\n";
1531 state->open_encoding_ = none;
1533 // Likewise for polyglossia or when using begin/end commands
1534 string const & cur_lang = openLanguageName(state);
1535 if (maintext && !is_child && !cur_lang.empty()) {
1536 os << from_utf8(subst(lang_end_command,
1540 if (using_begin_end)
1544 // reset inherited encoding
1545 if (state->cjk_inherited_ > 0) {
1546 state->cjk_inherited_ -= 1;
1547 if (state->cjk_inherited_ == 0)
1548 state->open_encoding_ = CJK;
1551 if (multibib_child && mparams.useBibtopic()) {
1552 os << "\\end{btUnit}\n";
1553 runparams.openbtUnit = false;
1558 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1559 OutputParams const & runparams, Encoding const & newEnc,
1560 bool force, bool noswitchmacro)
1562 // XeTeX/LuaTeX use only one encoding per document:
1563 // * with useNonTeXFonts: "utf8plain",
1564 // * with XeTeX and TeX fonts: "ascii" (inputenc fails),
1565 // * with LuaTeX and TeX fonts: only one encoding accepted by luainputenc.
1566 if (runparams.isFullUnicode() || newEnc.name() == "inherit")
1567 return make_pair(false, 0);
1569 Encoding const & oldEnc = *runparams.encoding;
1570 bool moving_arg = runparams.moving_arg;
1571 // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
1572 bool const from_to_cjk =
1573 (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
1574 || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
1575 if (!force && !from_to_cjk
1576 && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
1577 return make_pair(false, 0);
1579 // Do nothing if the encoding is unchanged.
1580 if (oldEnc.name() == newEnc.name())
1581 return make_pair(false, 0);
1583 // FIXME We ignore encoding switches from/to encodings that do
1584 // neither support the inputenc package nor the CJK package here.
1585 // This does of course only work in special cases (e.g. switch from
1586 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1587 // but it is the best we can do
1588 if (oldEnc.package() == Encoding::none
1589 || newEnc.package() == Encoding::none)
1590 return make_pair(false, 0);
1592 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1593 << oldEnc.name() << " to " << newEnc.name());
1594 os << setEncoding(newEnc.iconvName());
1595 if (bparams.inputenc == "default")
1596 return make_pair(true, 0);
1598 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1599 OutputState * state = getOutputState();
1600 switch (newEnc.package()) {
1601 case Encoding::none:
1602 case Encoding::japanese:
1603 // shouldn't ever reach here, see above
1604 return make_pair(true, 0);
1605 case Encoding::inputenc: {
1606 int count = inputenc_arg.length();
1607 if (oldEnc.package() == Encoding::CJK &&
1608 state->open_encoding_ == CJK) {
1610 state->open_encoding_ = none;
1613 else if (oldEnc.package() == Encoding::inputenc &&
1614 state->open_encoding_ == inputenc) {
1616 state->open_encoding_ = none;
1619 if (runparams.local_font != 0
1620 && oldEnc.package() == Encoding::CJK) {
1621 // within insets, \inputenc switches need
1622 // to be embraced within \bgroup...\egroup;
1626 state->open_encoding_ = inputenc;
1628 // with the japanese option, inputenc is omitted.
1629 if (runparams.use_japanese || noswitchmacro)
1630 return make_pair(true, count);
1631 os << "\\inputencoding{" << inputenc_arg << '}';
1632 return make_pair(true, count + 16);
1634 case Encoding::CJK: {
1635 int count = inputenc_arg.length();
1636 if (oldEnc.package() == Encoding::CJK &&
1637 state->open_encoding_ == CJK) {
1641 if (oldEnc.package() == Encoding::inputenc &&
1642 state->open_encoding_ == inputenc) {
1646 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1647 << from_ascii(bparams.fonts_cjk) << "}";
1648 state->open_encoding_ = CJK;
1649 return make_pair(true, count + 15);
1652 // Dead code to avoid a warning:
1653 return make_pair(true, 0);