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