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