Not so soon, I guess, since that FIXME was from r6305.
[lyx.git] / src / output_latex.cpp
bloba87416d7b7955e22066fcdeb797ab5465206ceca
1 /**
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.
9 */
11 #include <config.h>
13 #include "output_latex.h"
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "Encoding.h"
18 #include "InsetList.h"
19 #include "Language.h"
20 #include "Layout.h"
21 #include "LyXRC.h"
22 #include "OutputParams.h"
23 #include "Paragraph.h"
24 #include "ParagraphParameters.h"
25 #include "TextClass.h"
26 #include "TexRow.h"
27 #include "VSpace.h"
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetOptArg.h"
32 #include "support/lassert.h"
33 #include "support/debug.h"
34 #include "support/lstrings.h"
36 #include <boost/next_prior.hpp>
38 using namespace std;
39 using namespace lyx::support;
42 namespace lyx {
44 namespace {
46 enum OpenEncoding {
47 none,
48 inputenc,
49 CJK
52 static int open_encoding_ = none;
53 static int cjk_inherited_ = 0;
54 Language const * prev_env_language_ = 0;
57 ParagraphList::const_iterator
58 TeXEnvironment(Buffer const & buf,
59 Text const & text,
60 ParagraphList::const_iterator pit,
61 odocstream & os, TexRow & texrow,
62 OutputParams const & runparams);
65 ParagraphList::const_iterator
66 TeXDeeper(Buffer const & buf,
67 Text const & text,
68 ParagraphList::const_iterator pit,
69 odocstream & os, TexRow & texrow,
70 OutputParams const & runparams)
72 LYXERR(Debug::LATEX, "TeXDeeper... " << &*pit);
73 ParagraphList::const_iterator par = pit;
75 ParagraphList const & paragraphs = text.paragraphs();
77 bool const force_plain_layout = text.inset().forcePlainLayout();
78 while (par != paragraphs.end() &&
79 par->params().depth() == pit->params().depth()) {
80 // FIXME This test should not be necessary.
81 // We should perhaps issue an error if it is.
82 Layout const & style = force_plain_layout
83 ? buf.params().documentClass().plainLayout() : par->layout();
84 if (style.isEnvironment()) {
85 par = TeXEnvironment(buf, text, par,
86 os, texrow, runparams);
87 } else {
88 par = TeXOnePar(buf, text, par,
89 os, texrow, runparams);
92 LYXERR(Debug::LATEX, "TeXDeeper...done ");
94 return par;
98 ParagraphList::const_iterator
99 TeXEnvironment(Buffer const & buf,
100 Text const & text,
101 ParagraphList::const_iterator pit,
102 odocstream & os, TexRow & texrow,
103 OutputParams const & runparams)
105 LYXERR(Debug::LATEX, "TeXEnvironment... " << &*pit);
107 BufferParams const & bparams = buf.params();
109 // FIXME This test should not be necessary.
110 // We should perhaps issue an error if it is.
111 Layout const & style = text.inset().forcePlainLayout() ?
112 bparams.documentClass().plainLayout() : pit->layout();
114 ParagraphList const & paragraphs = text.paragraphs();
115 ParagraphList::const_iterator const priorpit =
116 pit == paragraphs.begin() ? pit : boost::prior(pit);
118 bool const use_prev_env_language = prev_env_language_ != 0
119 && priorpit->layout().isEnvironment()
120 && (priorpit->getDepth() > pit->getDepth()
121 || (priorpit->getDepth() == pit->getDepth()
122 && priorpit->layout() != pit->layout()));
124 Encoding const * const prev_encoding = runparams.encoding;
125 Language const * const par_language = pit->getParLanguage(bparams);
126 Language const * const doc_language = bparams.language;
127 Language const * const prev_par_language =
128 (pit != paragraphs.begin())
129 ? (use_prev_env_language ? prev_env_language_
130 : priorpit->getParLanguage(bparams))
131 : doc_language;
132 if (par_language->babel() != prev_par_language->babel()) {
134 if (!lyxrc.language_command_end.empty() &&
135 prev_par_language->babel() != doc_language->babel() &&
136 !prev_par_language->babel().empty()) {
137 os << from_ascii(subst(
138 lyxrc.language_command_end,
139 "$$lang",
140 prev_par_language->babel()))
141 // the '%' is necessary to prevent unwanted whitespace
142 << "%\n";
143 texrow.newline();
146 if ((lyxrc.language_command_end.empty() ||
147 par_language->babel() != doc_language->babel()) &&
148 !par_language->babel().empty()) {
149 os << from_ascii(subst(
150 lyxrc.language_command_begin,
151 "$$lang",
152 par_language->babel()))
153 // the '%' is necessary to prevent unwanted whitespace
154 << "%\n";
155 texrow.newline();
159 bool leftindent_open = false;
160 if (!pit->params().leftIndent().zero()) {
161 os << "\\begin{LyXParagraphLeftIndent}{"
162 << from_ascii(pit->params().leftIndent().asLatexString())
163 << "}\n";
164 texrow.newline();
165 leftindent_open = true;
168 if (style.isEnvironment()) {
169 os << "\\begin{" << from_ascii(style.latexname()) << '}';
170 if (style.optionalargs > 0) {
171 int ret = latexOptArgInsets(*pit, os, runparams,
172 style.optionalargs);
173 while (ret > 0) {
174 texrow.newline();
175 --ret;
178 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
179 os << '{'
180 << pit->params().labelWidthString()
181 << "}\n";
182 } else if (style.labeltype == LABEL_BIBLIO) {
183 if (pit->params().labelWidthString().empty())
184 os << '{' << bibitemWidest(buf) << "}\n";
185 else
186 os << '{'
187 << pit->params().labelWidthString()
188 << "}\n";
189 } else
190 os << from_ascii(style.latexparam()) << '\n';
191 texrow.newline();
194 // in multilingual environments, the CJK tags have to be nested properly
195 bool cjk_nested = false;
196 if (par_language->encoding()->package() == Encoding::CJK &&
197 open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
198 if (prev_par_language->encoding()->package() == Encoding::CJK)
199 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
200 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
201 open_encoding_ = CJK;
202 cjk_nested = true;
203 texrow.newline();
206 ParagraphList::const_iterator par = pit;
207 do {
208 par = TeXOnePar(buf, text, par, os, texrow, runparams);
210 if (par == paragraphs.end()) {
211 // Make sure that the last paragraph is
212 // correctly terminated (because TeXOnePar does
213 // not add a \n in this case)
214 os << '\n';
215 texrow.newline();
216 } else if (par->params().depth() > pit->params().depth()) {
217 if (par->layout().isParagraph()) {
218 // Thinko!
219 // How to handle this? (Lgb)
220 //&& !suffixIs(os, "\n\n")
222 // There should be at least one '\n' already
223 // but we need there to be two for Standard
224 // paragraphs that are depth-increment'ed to be
225 // output correctly. However, tables can
226 // also be paragraphs so don't adjust them.
227 // ARRae
228 // Thinkee:
229 // Will it ever harm to have one '\n' too
230 // many? i.e. that we sometimes will have
231 // three in a row. (Lgb)
232 os << '\n';
233 texrow.newline();
235 par = TeXDeeper(buf, text, par, os, texrow,
236 runparams);
238 } while (par != paragraphs.end()
239 && par->layout() == pit->layout()
240 && par->params().depth() == pit->params().depth()
241 && par->params().leftIndent() == pit->params().leftIndent());
243 if (open_encoding_ == CJK && cjk_nested) {
244 // We need to close the encoding even if it does not change
245 // to do correct environment nesting
246 os << "\\end{CJK}\n";
247 texrow.newline();
248 open_encoding_ = none;
251 if (style.isEnvironment()) {
252 os << "\\end{" << from_ascii(style.latexname()) << "}\n";
253 texrow.newline();
254 prev_env_language_ = par_language;
255 if (runparams.encoding != prev_encoding) {
256 runparams.encoding = prev_encoding;
257 if (!bparams.useXetex)
258 os << setEncoding(prev_encoding->iconvName());
262 if (leftindent_open) {
263 os << "\\end{LyXParagraphLeftIndent}\n";
264 texrow.newline();
265 prev_env_language_ = par_language;
266 if (runparams.encoding != prev_encoding) {
267 runparams.encoding = prev_encoding;
268 if (!bparams.useXetex)
269 os << setEncoding(prev_encoding->iconvName());
273 if (par != paragraphs.end())
274 LYXERR(Debug::LATEX, "TeXEnvironment...done " << &*par);
276 return par;
279 } // namespace anon
282 int latexOptArgInsets(Paragraph const & par, odocstream & os,
283 OutputParams const & runparams, int number)
285 int lines = 0;
287 InsetList::const_iterator it = par.insetList().begin();
288 InsetList::const_iterator end = par.insetList().end();
289 for (; it != end && number > 0 ; ++it) {
290 if (it->inset->lyxCode() == OPTARG_CODE) {
291 InsetOptArg * ins =
292 static_cast<InsetOptArg *>(it->inset);
293 lines += ins->latexOptional(os, runparams);
294 --number;
297 return lines;
300 // FIXME: this should be anonymous
301 ParagraphList::const_iterator TeXOnePar(Buffer const & buf,
302 Text const & text,
303 ParagraphList::const_iterator const pit,
304 odocstream & os, TexRow & texrow,
305 OutputParams const & runparams_in,
306 string const & everypar,
307 int start_pos, int end_pos)
309 LYXERR(Debug::LATEX, "TeXOnePar... " << &*pit << " '"
310 << everypar << "'");
311 BufferParams const & bparams = buf.params();
312 ParagraphList const & paragraphs = text.paragraphs();
314 ParagraphList::const_iterator const priorpit =
315 pit == paragraphs.begin() ? pit : boost::prior(pit);
316 ParagraphList::const_iterator const nextpit =
317 pit == paragraphs.end() ? pit : boost::next(pit);
319 OutputParams runparams = runparams_in;
320 runparams.isLastPar = nextpit == paragraphs.end();
322 if (runparams.verbatim) {
323 int const dist = distance(paragraphs.begin(), pit);
324 Font const outerfont = text.outerFont(dist);
326 // No newline if only one paragraph in this lyxtext
327 if (dist > 0) {
328 os << '\n';
329 texrow.newline();
332 /*bool need_par = */ pit->latex(bparams, outerfont,
333 os, texrow, runparams, start_pos, end_pos);
334 return nextpit;
337 // FIXME This check should not really be needed.
338 // Perhaps we should issue an error if it is.
339 Layout const style = text.inset().forcePlainLayout() ?
340 bparams.documentClass().plainLayout() : pit->layout();
342 runparams.moving_arg |= style.needprotect;
344 bool const maintext = text.isMainText();
345 // we are at the beginning of an inset and CJK is already open;
346 // we count inheritation levels to get the inset nesting right.
347 if (pit == paragraphs.begin() && !maintext
348 && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
349 cjk_inherited_ += 1;
350 open_encoding_ = none;
353 // This paragraph's language
354 Language const * const par_language = pit->getParLanguage(bparams);
355 // The document's language
356 Language const * const doc_language = bparams.language;
357 // The language that was in effect when the environment this paragraph is
358 // inside of was opened
359 Language const * const outer_language =
360 (runparams.local_font != 0) ?
361 runparams.local_font->language() : doc_language;
363 // The previous language that was in effect is the language of the
364 // previous paragraph, unless the previous paragraph is inside an
365 // environment with nesting depth greater than (or equal to, but with
366 // a different layout) the current one. If there is no previous
367 // paragraph, the previous language is the outer language.
368 bool const use_prev_env_language = prev_env_language_ != 0
369 && priorpit->layout().isEnvironment()
370 && (priorpit->getDepth() > pit->getDepth()
371 || (priorpit->getDepth() == pit->getDepth()
372 && priorpit->layout() != pit->layout()));
373 Language const * const prev_language =
374 (pit != paragraphs.begin())
375 ? (use_prev_env_language ? prev_env_language_
376 : priorpit->getParLanguage(bparams))
377 : outer_language;
379 if (par_language->babel() != prev_language->babel()
380 // check if we already put language command in TeXEnvironment()
381 && !(style.isEnvironment()
382 && (pit == paragraphs.begin() ||
383 (priorpit->layout() != pit->layout() &&
384 priorpit->getDepth() <= pit->getDepth())
385 || priorpit->getDepth() < pit->getDepth())))
387 if (!lyxrc.language_command_end.empty() &&
388 prev_language->babel() != outer_language->babel() &&
389 !prev_language->babel().empty())
391 os << from_ascii(subst(lyxrc.language_command_end,
392 "$$lang",
393 prev_language->babel()))
394 // the '%' is necessary to prevent unwanted whitespace
395 << "%\n";
396 texrow.newline();
399 // We need to open a new language if we couldn't close the previous
400 // one (because there's no language_command_end); and even if we closed
401 // the previous one, if the current language is different than the
402 // outer_language (which is currently in effect once the previous one
403 // is closed).
404 if ((lyxrc.language_command_end.empty() ||
405 par_language->babel() != outer_language->babel()) &&
406 !par_language->babel().empty()) {
407 // If we're inside an inset, and that inset is within an \L or \R
408 // (or equivalents), then within the inset, too, any opposite
409 // language paragraph should appear within an \L or \R (in addition
410 // to, outside of, the normal language switch commands).
411 // This behavior is not correct for ArabTeX, though.
412 if ( // not for ArabTeX
413 (par_language->lang() != "arabic_arabtex" &&
414 outer_language->lang() != "arabic_arabtex") &&
415 // are we in an inset?
416 runparams.local_font != 0 &&
417 // is the inset within an \L or \R?
419 // FIXME: currently, we don't check this; this means that
420 // we'll have unnnecessary \L and \R commands, but that
421 // doesn't seem to hurt (though latex will complain)
423 // is this paragraph in the opposite direction?
424 runparams.local_font->isRightToLeft() !=
425 par_language->rightToLeft()
427 // FIXME: I don't have a working copy of the Arabi package, so
428 // I'm not sure if the farsi and arabic_arabi stuff is correct
429 // or not...
430 if (par_language->lang() == "farsi")
431 os << "\\textFR{";
432 else if (outer_language->lang() == "farsi")
433 os << "\\textLR{";
434 else if (par_language->lang() == "arabic_arabi")
435 os << "\\textAR{";
436 else if (outer_language->lang() == "arabic_arabi")
437 os << "\\textLR{";
438 // remaining RTL languages currently is hebrew
439 else if (par_language->rightToLeft())
440 os << "\\R{";
441 else
442 os << "\\L{";
444 // With CJK, the CJK tag has to be closed first (see below)
445 if (runparams.encoding->package() != Encoding::CJK) {
446 os << from_ascii(subst(
447 lyxrc.language_command_begin,
448 "$$lang",
449 par_language->babel()))
450 // the '%' is necessary to prevent unwanted whitespace
451 << "%\n";
452 texrow.newline();
457 // Switch file encoding if necessary; no need to do this for "default"
458 // encoding, since this only affects the position of the outputted
459 // \inputencoding command; the encoding switch will occur when necessary
460 if (bparams.inputenc == "auto" &&
461 runparams.encoding->package() != Encoding::none) {
462 // Look ahead for future encoding changes.
463 // We try to output them at the beginning of the paragraph,
464 // since the \inputencoding command is not allowed e.g. in
465 // sections.
466 for (pos_type i = 0; i < pit->size(); ++i) {
467 char_type const c = pit->getChar(i);
468 Encoding const * const encoding =
469 pit->getFontSettings(bparams, i).language()->encoding();
470 if (encoding->package() != Encoding::CJK &&
471 runparams.encoding->package() == Encoding::inputenc &&
472 c < 0x80)
473 continue;
474 if (pit->isInset(i))
475 break;
476 // All characters before c are in the ASCII range, and
477 // c is non-ASCII (but no inset), so change the
478 // encoding to that required by the language of c.
479 // With CJK, only add switch if we have CJK content at the beginning
480 // of the paragraph
481 if (encoding->package() != Encoding::CJK || i == 0) {
482 pair<bool, int> enc_switch = switchEncoding(os, bparams, runparams,
483 *encoding);
484 // the following is necessary after a CJK environment in a multilingual
485 // context (nesting issue).
486 if (par_language->encoding()->package() == Encoding::CJK &&
487 open_encoding_ != CJK && cjk_inherited_ == 0) {
488 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
489 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
490 open_encoding_ = CJK;
491 texrow.newline();
493 if (encoding->package() != Encoding::none && enc_switch.first) {
494 if (enc_switch.second > 0) {
495 // the '%' is necessary to prevent unwanted whitespace
496 os << "%\n";
497 texrow.newline();
499 // With CJK, the CJK tag had to be closed first (see above)
500 if (runparams.encoding->package() == Encoding::CJK) {
501 os << from_ascii(subst(
502 lyxrc.language_command_begin,
503 "$$lang",
504 par_language->babel()))
505 // the '%' is necessary to prevent unwanted whitespace
506 << "%\n";
507 texrow.newline();
509 runparams.encoding = encoding;
511 break;
516 Encoding const * const prev_encoding = runparams.encoding;
518 bool const useSetSpace = bparams.documentClass().provides("SetSpace");
519 if (pit->allowParagraphCustomization()) {
520 if (pit->params().startOfAppendix()) {
521 os << "\\appendix\n";
522 texrow.newline();
525 if (!pit->params().spacing().isDefault()
526 && (pit == paragraphs.begin()
527 || !priorpit->hasSameLayout(*pit)))
529 os << from_ascii(pit->params().spacing().writeEnvirBegin(useSetSpace))
530 << '\n';
531 texrow.newline();
534 if (style.isCommand()) {
535 os << '\n';
536 texrow.newline();
540 switch (style.latextype) {
541 case LATEX_COMMAND:
542 os << '\\' << from_ascii(style.latexname());
544 // Separate handling of optional argument inset.
545 if (style.optionalargs > 0) {
546 int ret = latexOptArgInsets(*pit, os, runparams,
547 style.optionalargs);
548 while (ret > 0) {
549 texrow.newline();
550 --ret;
553 else
554 os << from_ascii(style.latexparam());
555 break;
556 case LATEX_ITEM_ENVIRONMENT:
557 case LATEX_LIST_ENVIRONMENT:
558 os << "\\item ";
559 break;
560 case LATEX_BIB_ENVIRONMENT:
561 // ignore this, the inset will write itself
562 break;
563 default:
564 break;
567 Font const outerfont = text.outerFont(distance(paragraphs.begin(), pit));
569 // FIXME UNICODE
570 os << from_utf8(everypar);
571 bool need_par = pit->latex(bparams, outerfont,
572 os, texrow, runparams, start_pos, end_pos);
574 // Make sure that \\par is done with the font of the last
575 // character if this has another size as the default.
576 // This is necessary because LaTeX (and LyX on the screen)
577 // calculates the space between the baselines according
578 // to this font. (Matthias)
580 // Is this really needed ? (Dekel)
581 // We do not need to use to change the font for the last paragraph
582 // or for a command.
584 Font const font = pit->empty()
585 ? pit->getLayoutFont(bparams, outerfont)
586 : pit->getFont(bparams, pit->size() - 1, outerfont);
588 bool const is_command = style.isCommand();
590 if (style.resfont.size() != font.fontInfo().size()
591 && nextpit != paragraphs.end()
592 && !is_command) {
593 if (!need_par)
594 os << '{';
595 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
596 } else if (need_par) {
597 os << "\\par}";
598 } else if (is_command) {
599 os << '}';
600 if (runparams.encoding != prev_encoding) {
601 runparams.encoding = prev_encoding;
602 if (!bparams.useXetex)
603 os << setEncoding(prev_encoding->iconvName());
607 bool pending_newline = false;
608 switch (style.latextype) {
609 case LATEX_ITEM_ENVIRONMENT:
610 case LATEX_LIST_ENVIRONMENT:
611 if (nextpit != paragraphs.end()
612 && (pit->params().depth() < nextpit->params().depth()))
613 pending_newline = true;
614 break;
615 case LATEX_ENVIRONMENT: {
616 // if its the last paragraph of the current environment
617 // skip it otherwise fall through
618 if (nextpit != paragraphs.end() &&
619 (nextpit->layout() != pit->layout()
620 || nextpit->params().depth() != pit->params().depth()))
621 break;
624 // fall through possible
625 default:
626 // we don't need it for the last paragraph!!!
627 if (nextpit != paragraphs.end())
628 pending_newline = true;
631 if (pit->allowParagraphCustomization()) {
632 if (!pit->params().spacing().isDefault()
633 && (nextpit == paragraphs.end() || !nextpit->hasSameLayout(*pit)))
635 if (pending_newline) {
636 os << '\n';
637 texrow.newline();
639 os << from_ascii(pit->params().spacing().writeEnvirEnd(useSetSpace));
640 pending_newline = true;
644 // Closing the language is needed for the last paragraph; it is also
645 // needed if we're within an \L or \R that we may have opened above (not
646 // necessarily in this paragraph) and are about to close.
647 bool closing_rtl_ltr_environment =
648 // not for ArabTeX
649 (par_language->lang() != "arabic_arabtex" &&
650 outer_language->lang() != "arabic_arabtex") &&
651 // have we opened and \L or \R environment?
652 runparams.local_font != 0 &&
653 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
654 // are we about to close the language?
655 ((nextpit != paragraphs.end() &&
656 par_language->babel() !=
657 (nextpit->getParLanguage(bparams))->babel()) ||
658 (nextpit == paragraphs.end() &&
659 par_language->babel() != outer_language->babel()));
661 if (closing_rtl_ltr_environment || (nextpit == paragraphs.end()
662 && par_language->babel() != outer_language->babel())) {
663 // Since \selectlanguage write the language to the aux file,
664 // we need to reset the language at the end of footnote or
665 // float.
667 if (pending_newline) {
668 os << '\n';
669 texrow.newline();
671 // when the paragraph uses CJK, the language has to be closed earlier
672 if (font.language()->encoding()->package() != Encoding::CJK) {
673 if (lyxrc.language_command_end.empty()) {
674 // If this is a child, we should restore the
675 // master language after the last paragraph.
676 Language const * const current_language =
677 (nextpit == paragraphs.end()
678 && runparams.master_language)
679 ? runparams.master_language
680 : prev_language;
681 if (!current_language->babel().empty()) {
682 os << from_ascii(subst(
683 lyxrc.language_command_begin,
684 "$$lang",
685 current_language->babel()));
686 pending_newline = true;
688 } else if (!par_language->babel().empty()) {
689 os << from_ascii(subst(
690 lyxrc.language_command_end,
691 "$$lang",
692 par_language->babel()));
693 pending_newline = true;
697 if (closing_rtl_ltr_environment)
698 os << "}";
700 if (pending_newline) {
701 os << '\n';
702 texrow.newline();
705 // if this is a CJK-paragraph and the next isn't, close CJK
706 // also if the next paragraph is a multilingual environment (because of nesting)
707 if (nextpit != paragraphs.end() && open_encoding_ == CJK &&
708 (nextpit->getParLanguage(bparams)->encoding()->package() != Encoding::CJK ||
709 (nextpit->layout().isEnvironment() && nextpit->isMultiLingual(bparams)))
710 // inbetween environments, CJK has to be closed later (nesting!)
711 && (!style.isEnvironment() || !nextpit->layout().isEnvironment())) {
712 os << "\\end{CJK}\n";
713 open_encoding_ = none;
716 // If this is the last paragraph, close the CJK environment
717 // if necessary. If it's an environment, we'll have to \end that first.
718 if (nextpit == paragraphs.end() && !style.isEnvironment()) {
719 switch (open_encoding_) {
720 case CJK: {
721 // do nothing at the end of child documents
722 if (maintext && buf.masterBuffer() != &buf)
723 break;
724 // end of main text
725 if (maintext) {
726 os << '\n';
727 texrow.newline();
728 os << "\\end{CJK}\n";
729 texrow.newline();
730 // end of an inset
731 } else
732 os << "\\end{CJK}";
733 open_encoding_ = none;
734 break;
736 case inputenc: {
737 os << "\\egroup";
738 open_encoding_ = none;
739 break;
741 case none:
742 default:
743 // do nothing
744 break;
748 // If this is the last paragraph, and a local_font was set upon entering
749 // the inset, and we're using "auto" or "default" encoding, the encoding
750 // should be set back to that local_font's encoding.
751 // However, do not change the encoding when XeTeX is used.
752 if (nextpit == paragraphs.end() && runparams_in.local_font != 0
753 && runparams_in.encoding != runparams_in.local_font->language()->encoding()
754 && (bparams.inputenc == "auto" || bparams.inputenc == "default")
755 && (!bparams.useXetex)) {
756 runparams_in.encoding = runparams_in.local_font->language()->encoding();
757 os << setEncoding(runparams_in.encoding->iconvName());
759 // Otherwise, the current encoding should be set for the next paragraph.
760 else
761 runparams_in.encoding = runparams.encoding;
764 // we don't need a newline for the last paragraph!!!
765 // Note from JMarc: we will re-add a \n explicitly in
766 // TeXEnvironment, because it is needed in this case
767 if (nextpit != paragraphs.end()) {
768 Layout const & next_layout = nextpit->layout();
769 if (style == next_layout
770 // no blank lines before environments!
771 || !next_layout.isEnvironment()
772 // unless there's a depth change
773 // FIXME What we really want to do here is put every \begin and \end
774 // tag on a new line (which was not the case with nested environments).
775 // But in the present state of play, we don't have access to the
776 // information whether the current TeX row is empty or not.
777 // For some ideas about how to fix this, see this thread:
778 // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
779 || nextpit->params().depth() != pit->params().depth()) {
780 os << '\n';
781 texrow.newline();
785 if (nextpit != paragraphs.end())
786 LYXERR(Debug::LATEX, "TeXOnePar...done " << &*nextpit);
788 return nextpit;
792 // LaTeX all paragraphs
793 void latexParagraphs(Buffer const & buf,
794 Text const & text,
795 odocstream & os,
796 TexRow & texrow,
797 OutputParams const & runparams,
798 string const & everypar)
800 bool was_title = false;
801 bool already_title = false;
802 BufferParams const & bparams = buf.params();
803 DocumentClass const & tclass = bparams.documentClass();
804 ParagraphList const & paragraphs = text.paragraphs();
805 ParagraphList::const_iterator par = paragraphs.begin();
806 ParagraphList::const_iterator endpar = paragraphs.end();
808 LASSERT(runparams.par_begin <= runparams.par_end, /**/);
809 // if only part of the paragraphs will be outputed
810 if (runparams.par_begin != runparams.par_end) {
811 par = boost::next(paragraphs.begin(), runparams.par_begin);
812 endpar = boost::next(paragraphs.begin(), runparams.par_end);
813 // runparams will be passed to nested paragraphs, so
814 // we have to reset the range parameters.
815 const_cast<OutputParams&>(runparams).par_begin = 0;
816 const_cast<OutputParams&>(runparams).par_end = 0;
819 bool const maintext = text.isMainText();
820 bool const is_child = buf.masterBuffer() != &buf;
822 // Open a CJK environment at the beginning of the main buffer
823 // if the document's language is a CJK language
824 // (but not in child documents)
825 if (maintext && !is_child
826 && bparams.encoding().package() == Encoding::CJK) {
827 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
828 << "}{" << from_ascii(bparams.fontsCJK) << "}%\n";
829 texrow.newline();
830 open_encoding_ = CJK;
832 // if "auto begin" is switched off, explicitely switch the
833 // language on at start
834 if (maintext && !lyxrc.language_auto_begin &&
835 !bparams.language->babel().empty()) {
836 // FIXME UNICODE
837 os << from_utf8(subst(lyxrc.language_command_begin,
838 "$$lang",
839 bparams.language->babel()))
840 << '\n';
841 texrow.newline();
844 ParagraphList::const_iterator lastpar;
845 // if only_body
846 while (par != endpar) {
847 lastpar = par;
848 // FIXME This check should not be needed. We should
849 // perhaps issue an error if it is.
850 Layout const & layout = text.inset().forcePlainLayout() ?
851 tclass.plainLayout() : par->layout();
853 if (layout.intitle) {
854 if (already_title) {
855 lyxerr << "Error in latexParagraphs: You"
856 " should not mix title layouts"
857 " with normal ones." << endl;
858 } else if (!was_title) {
859 was_title = true;
860 if (tclass.titletype() == TITLE_ENVIRONMENT) {
861 os << "\\begin{"
862 << from_ascii(tclass.titlename())
863 << "}\n";
864 texrow.newline();
867 } else if (was_title && !already_title) {
868 if (tclass.titletype() == TITLE_ENVIRONMENT) {
869 os << "\\end{" << from_ascii(tclass.titlename())
870 << "}\n";
872 else {
873 os << "\\" << from_ascii(tclass.titlename())
874 << "\n";
876 texrow.newline();
877 already_title = true;
878 was_title = false;
881 if (layout.isEnvironment() ||
882 !par->params().leftIndent().zero()) {
883 par = TeXEnvironment(buf, text, par, os,
884 texrow, runparams);
885 } else {
886 par = TeXOnePar(buf, text, par, os, texrow,
887 runparams, everypar);
889 if (distance(lastpar, par) >= distance(lastpar, endpar))
890 break;
893 // It might be that we only have a title in this document
894 if (was_title && !already_title) {
895 if (tclass.titletype() == TITLE_ENVIRONMENT) {
896 os << "\\end{" << from_ascii(tclass.titlename())
897 << "}\n";
899 else {
900 os << "\\" << from_ascii(tclass.titlename())
901 << "\n";
903 texrow.newline();
906 // if "auto end" is switched off, explicitely close the language at the end
907 // but only if the last par is in a babel language
908 if (maintext && !lyxrc.language_auto_end && !bparams.language->babel().empty() &&
909 lastpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
910 os << from_utf8(subst(lyxrc.language_command_end,
911 "$$lang",
912 bparams.language->babel()))
913 << '\n';
914 texrow.newline();
917 // If the last paragraph is an environment, we'll have to close
918 // CJK at the very end to do proper nesting.
919 if (maintext && !is_child && open_encoding_ == CJK) {
920 os << "\\end{CJK}\n";
921 texrow.newline();
922 open_encoding_ = none;
925 // reset inherited encoding
926 if (cjk_inherited_ > 0) {
927 cjk_inherited_ -= 1;
928 if (cjk_inherited_ == 0)
929 open_encoding_ = CJK;
934 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
935 OutputParams const & runparams, Encoding const & newEnc,
936 bool force)
938 Encoding const & oldEnc = *runparams.encoding;
939 bool moving_arg = runparams.moving_arg;
940 if (!force && ((bparams.inputenc != "auto" && bparams.inputenc != "default")
941 || moving_arg))
942 return make_pair(false, 0);
944 // Do nothing if the encoding is unchanged.
945 if (oldEnc.name() == newEnc.name())
946 return make_pair(false, 0);
948 // FIXME We ignore encoding switches from/to encodings that do
949 // neither support the inputenc package nor the CJK package here.
950 // This does of course only work in special cases (e.g. switch from
951 // tis620-0 to latin1, but the text in latin1 contains ASCII only),
952 // but it is the best we can do
953 if (oldEnc.package() == Encoding::none
954 || newEnc.package() == Encoding::none)
955 return make_pair(false, 0);
957 LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
958 << oldEnc.name() << " to " << newEnc.name());
959 os << setEncoding(newEnc.iconvName());
960 if (bparams.inputenc == "default")
961 return make_pair(true, 0);
963 docstring const inputenc_arg(from_ascii(newEnc.latexName()));
964 switch (newEnc.package()) {
965 case Encoding::none:
966 case Encoding::japanese:
967 // shouldn't ever reach here, see above
968 return make_pair(true, 0);
969 case Encoding::inputenc: {
970 int count = inputenc_arg.length();
971 if (oldEnc.package() == Encoding::CJK &&
972 open_encoding_ == CJK) {
973 os << "\\end{CJK}";
974 open_encoding_ = none;
975 count += 9;
977 else if (oldEnc.package() == Encoding::inputenc &&
978 open_encoding_ == inputenc) {
979 os << "\\egroup";
980 open_encoding_ = none;
981 count += 7;
983 if (runparams.local_font != 0
984 && oldEnc.package() == Encoding::CJK) {
985 // within insets, \inputenc switches need
986 // to be embraced within \bgroup...\egroup;
987 // else CJK fails.
988 os << "\\bgroup";
989 count += 7;
990 open_encoding_ = inputenc;
992 // with the japanese option, inputenc is omitted.
993 if (runparams.use_japanese)
994 return make_pair(true, count);
995 os << "\\inputencoding{" << inputenc_arg << '}';
996 return make_pair(true, count + 16);
998 case Encoding::CJK: {
999 int count = inputenc_arg.length();
1000 if (oldEnc.package() == Encoding::CJK &&
1001 open_encoding_ == CJK) {
1002 os << "\\end{CJK}";
1003 count += 9;
1005 if (oldEnc.package() == Encoding::inputenc &&
1006 open_encoding_ == inputenc) {
1007 os << "\\egroup";
1008 count += 7;
1010 os << "\\begin{CJK}{" << inputenc_arg << "}{"
1011 << from_ascii(bparams.fontsCJK) << "}";
1012 open_encoding_ = CJK;
1013 return make_pair(true, count + 15);
1016 // Dead code to avoid a warning:
1017 return make_pair(true, 0);
1021 } // namespace lyx