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