]> git.lyx.org Git - lyx.git/blob - src/output_latex.cpp
* gcc does not like missing characters in keywords
[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 "debug.h"
18 #include "Encoding.h"
19 #include "InsetList.h"
20 #include "Language.h"
21 #include "Layout.h"
22 #include "LyXRC.h"
23 #include "OutputParams.h"
24 #include "Paragraph.h"
25 #include "paragraph_funcs.h"
26 #include "ParagraphParameters.h"
27 #include "TexRow.h"
28 #include "VSpace.h"
29
30 #include "insets/InsetBibitem.h"
31 #include "insets/InsetOptArg.h"
32
33 #include "support/lstrings.h"
34
35 #include <boost/next_prior.hpp>
36
37 namespace lyx {
38
39 using support::subst;
40
41 using std::endl;
42 using std::string;
43 using std::pair;
44 using std::make_pair;
45
46
47 namespace {
48
49 ParagraphList::const_iterator
50 TeXEnvironment(Buffer const & buf,
51                ParagraphList const & paragraphs,
52                ParagraphList::const_iterator pit,
53                odocstream & os, TexRow & texrow,
54                OutputParams const & runparams);
55
56 ParagraphList::const_iterator
57 TeXOnePar(Buffer const & buf,
58           ParagraphList const & paragraphs,
59           ParagraphList::const_iterator pit,
60           odocstream & os, TexRow & texrow,
61           OutputParams const & runparams,
62           string const & everypar = string());
63
64
65 ParagraphList::const_iterator
66 TeXDeeper(Buffer const & buf,
67           ParagraphList const & paragraphs,
68           ParagraphList::const_iterator pit,
69           odocstream & os, TexRow & texrow,
70           OutputParams const & runparams)
71 {
72         LYXERR(Debug::LATEX) << "TeXDeeper...     " << &*pit << endl;
73         ParagraphList::const_iterator par = pit;
74
75         while (par != paragraphs.end() &&
76                      par->params().depth() == pit->params().depth()) {
77                 if (par->layout()->isEnvironment()) {
78                         par = TeXEnvironment(buf, paragraphs, par,
79                                              os, texrow, runparams);
80                 } else {
81                         par = TeXOnePar(buf, paragraphs, par,
82                                              os, texrow, runparams);
83                 }
84         }
85         LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
86
87         return par;
88 }
89
90
91 ParagraphList::const_iterator
92 TeXEnvironment(Buffer const & buf,
93                ParagraphList const & paragraphs,
94                ParagraphList::const_iterator pit,
95                odocstream & os, TexRow & texrow,
96                OutputParams const & runparams)
97 {
98         LYXERR(Debug::LATEX) << "TeXEnvironment...     " << &*pit << endl;
99
100         BufferParams const & bparams = buf.params();
101
102         LayoutPtr const & style = pit->layout();
103
104         Language const * const par_language = pit->getParLanguage(bparams);
105         Language const * const doc_language = bparams.language;
106         Language const * const prev_par_language =
107                 (pit != paragraphs.begin())
108                 ? boost::prior(pit)->getParLanguage(bparams)
109                 : doc_language;
110         if (par_language->babel() != prev_par_language->babel()) {
111
112                 if (!lyxrc.language_command_end.empty() &&
113                     prev_par_language->babel() != doc_language->babel() &&
114                     !prev_par_language->babel().empty()) {
115                         os << from_ascii(subst(
116                                 lyxrc.language_command_end,
117                                 "$$lang",
118                                 prev_par_language->babel()))
119                            << '\n';
120                         texrow.newline();
121                 }
122
123                 if ((lyxrc.language_command_end.empty() ||
124                      par_language->babel() != doc_language->babel()) &&
125                     !par_language->babel().empty()) {
126                         os << from_ascii(subst(
127                                 lyxrc.language_command_begin,
128                                 "$$lang",
129                                 par_language->babel()))
130                            << '\n';
131                         texrow.newline();
132                 }
133         }
134
135         bool leftindent_open = false;
136         if (!pit->params().leftIndent().zero()) {
137                 os << "\\begin{LyXParagraphLeftIndent}{"
138                    << from_ascii(pit->params().leftIndent().asLatexString())
139                    << "}\n";
140                 texrow.newline();
141                 leftindent_open = true;
142         }
143
144         if (style->isEnvironment()) {
145                 os << "\\begin{" << from_ascii(style->latexname()) << '}';
146                 if (style->optionalargs > 0) {
147                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
148                                                     style->optionalargs);
149                         while (ret > 0) {
150                                 texrow.newline();
151                                 --ret;
152                         }
153                 }
154                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
155                         os << '{'
156                            << pit->params().labelWidthString()
157                            << "}\n";
158                 } else if (style->labeltype == LABEL_BIBLIO) {
159                         // ale970405
160                         os << '{' << bibitemWidest(buf) << "}\n";
161                 } else
162                         os << from_ascii(style->latexparam()) << '\n';
163                 texrow.newline();
164         }
165         ParagraphList::const_iterator par = pit;
166         do {
167                 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
168
169                 if (par == paragraphs.end()) {
170                         // Make sure that the last paragraph is
171                         // correctly terminated (because TeXOnePar does
172                         // not add a \n in this case)
173                         os << '\n';
174                         texrow.newline();
175                 } else if (par->params().depth() > pit->params().depth()) {
176                         if (par->layout()->isParagraph()) {
177                           // Thinko!
178                           // How to handle this? (Lgb)
179                           //&& !suffixIs(os, "\n\n")
180                                   //) {
181
182                                 // There should be at least one '\n' already
183                                 // but we need there to be two for Standard
184                                 // paragraphs that are depth-increment'ed to be
185                                 // output correctly.  However, tables can
186                                 // also be paragraphs so don't adjust them.
187                                 // ARRae
188                                 // Thinkee:
189                                 // Will it ever harm to have one '\n' too
190                                 // many? i.e. that we sometimes will have
191                                 // three in a row. (Lgb)
192                                 os << '\n';
193                                 texrow.newline();
194                         }
195                         par = TeXDeeper(buf, paragraphs, par, os, texrow,
196                                         runparams);
197                 }
198         } while (par != paragraphs.end()
199                  && par->layout() == pit->layout()
200                  && par->params().depth() == pit->params().depth()
201                  && par->params().leftIndent() == pit->params().leftIndent());
202
203         if (style->isEnvironment()) {
204                 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
205                 texrow.newline();
206         }
207
208         if (leftindent_open) {
209                 os << "\\end{LyXParagraphLeftIndent}\n";
210                 texrow.newline();
211         }
212
213         if (par != paragraphs.end()) {
214                 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
215         }
216         return par;
217 }
218
219 }
220
221
222 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
223                       odocstream & os, OutputParams const & runparams, int number)
224 {
225         int lines = 0;
226
227         InsetList::const_iterator it = par.insetList().begin();
228         InsetList::const_iterator end = par.insetList().end();
229         for (; it != end && number > 0 ; ++it) {
230                 if (it->inset->lyxCode() == OPTARG_CODE) {
231                         InsetOptArg * ins =
232                                 static_cast<InsetOptArg *>(it->inset);
233                         lines += ins->latexOptional(buf, os, runparams);
234                         --number;
235                 }
236         }
237         return lines;
238 }
239
240
241 namespace {
242
243 ParagraphList::const_iterator
244 TeXOnePar(Buffer const & buf,
245           ParagraphList const & paragraphs,
246           ParagraphList::const_iterator pit,
247           odocstream & os, TexRow & texrow,
248           OutputParams const & runparams_in,
249           string const & everypar)
250 {
251         LYXERR(Debug::LATEX) << "TeXOnePar...     " << &*pit << " '"
252                 << everypar << "'" << endl;
253         BufferParams const & bparams = buf.params();
254         LayoutPtr style;
255
256         if (runparams_in.verbatim) {
257                 int const dist = std::distance(paragraphs.begin(), pit);
258                 Font const outerfont = outerFont(dist, paragraphs);
259
260                 // No newline if only one paragraph in this lyxtext
261                 if (dist > 0) {
262                         os << '\n';
263                         texrow.newline();
264                 }
265
266                 /*bool need_par = */ pit->latex(buf, bparams, outerfont,
267                         os, texrow, runparams_in);
268
269                 return ++pit;
270         }
271
272         // In an inset with unlimited length (all in one row),
273         // force layout to default
274         if (!pit->forceDefaultParagraphs())
275                 style = pit->layout();
276         else
277                 style = bparams.getTextClass().defaultLayout();
278
279         OutputParams runparams = runparams_in;
280         runparams.moving_arg |= style->needprotect;
281
282         // This paragraph's language
283         Language const * const par_language = pit->getParLanguage(bparams);
284         // The document's language
285         Language const * const doc_language = bparams.language;
286         // The language that was in effect when the environemnt this paragraph is 
287         // inside of was opened
288         Language const * const outer_language = 
289                 (runparams.local_font != 0) ?
290                         runparams.local_font->language() : doc_language;
291         // The previous language that was in effect is either the language of
292         // the previous paragraph, if there is one, or else the outer language
293         // if there is no previous paragraph
294         Language const * const prev_language =
295                 (pit != paragraphs.begin()) ?
296                         boost::prior(pit)->getParLanguage(bparams) : outer_language;
297
298         if (par_language->babel() != prev_language->babel()
299             // check if we already put language command in TeXEnvironment()
300             && !(style->isEnvironment()
301                  && (pit == paragraphs.begin() ||
302                      (boost::prior(pit)->layout() != pit->layout() &&
303                       boost::prior(pit)->getDepth() <= pit->getDepth())
304                      || boost::prior(pit)->getDepth() < pit->getDepth())))
305         {
306                 if (!lyxrc.language_command_end.empty() &&
307                     prev_language->babel() != outer_language->babel() &&
308                     !prev_language->babel().empty())
309                 {
310                         os << from_ascii(subst(lyxrc.language_command_end,
311                                 "$$lang",
312                                 prev_language->babel()))
313                            << '\n';
314                         texrow.newline();
315                 }
316
317                 // We need to open a new language if we couldn't close the previous 
318                 // one (because there's no language_command_end); and even if we closed
319                 // the previous one, if the current language is different than the
320                 // outer_language (which is currently in effect once the previous one
321                 // is closed).
322                 if ((lyxrc.language_command_end.empty() ||
323                      par_language->babel() != outer_language->babel()) &&
324                     !par_language->babel().empty()) {
325                         // If we're inside an inset, and that inset is within an \L or \R
326                         // (or equivalents), then within the inset, too, any opposite
327                         // language paragraph should appear within an \L or \R (in addition
328                         // to, outside of, the normal language switch commands).
329                         // This behavior is not correct for ArabTeX, though.
330                         if (    // not for ArabTeX
331                                         (par_language->lang() != "arabic_arabtex" &&
332                                          outer_language->lang() != "arabic_arabtex") &&
333                                         // are we in an inset?
334                                         runparams.local_font != 0 &&
335                                         // is the inset within an \L or \R?
336                                         // 
337                                         // FIXME: currently, we don't check this; this means that
338                                         // we'll have unnnecessary \L and \R commands, but that 
339                                         // doesn't seem to hurt (though latex will complain)
340                                         // 
341                                         // is this paragraph in the opposite direction?
342                                         runparams.local_font->isRightToLeft() !=
343                                                 par_language->rightToLeft()
344                                 ) {
345                                 // FIXME: I don't have a working copy of the Arabi package, so
346                                 // I'm not sure if the farsi and arabic_arabi stuff is correct
347                                 // or not...
348                                 if (par_language->lang() == "farsi")
349                                         os << "\\textFR{";
350                                 else if (outer_language->lang() == "farsi")
351                                         os << "\\textLR{";
352                                 else if (par_language->lang() == "arabic_arabi")
353                                         os << "\\textAR{";
354                                 else if (outer_language->lang() == "arabic_arabi")
355                                         os << "\\textLR{";
356                                 // remaining RTL languages currently is hebrew
357                                 else if (par_language->rightToLeft())
358                                         os << "\\R{";
359                                 else
360                                         os << "\\L{";
361                         }
362                         os << from_ascii(subst(
363                                 lyxrc.language_command_begin,
364                                 "$$lang",
365                                 par_language->babel()))
366                            << '\n';
367                         texrow.newline();
368                 }
369         }
370
371         // Switch file encoding if necessary; no need to do this for "default"
372         // encoding, since this only affects the position of the outputted
373         // \inputencoding command; the encoding switch will occur when necessary
374         if (bparams.inputenc == "auto" &&
375             runparams.encoding->package() == Encoding::inputenc) {
376                 // Look ahead for future encoding changes.
377                 // We try to output them at the beginning of the paragraph,
378                 // since the \inputencoding command is not allowed e.g. in
379                 // sections.
380                 for (pos_type i = 0; i < pit->size(); ++i) {
381                         char_type const c = pit->getChar(i);
382                         if (c < 0x80)
383                                 continue;
384                         if (pit->isInset(i))
385                                 break;
386                         // All characters before c are in the ASCII range, and
387                         // c is non-ASCII (but no inset), so change the
388                         // encoding to that required by the language of c.
389                         Encoding const * const encoding =
390                                 pit->getFontSettings(bparams, i).language()->encoding();
391                         pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
392                                         *(runparams.encoding), *encoding);
393                         if (encoding->package() == Encoding::inputenc && enc_switch.first) {
394                                 runparams.encoding = encoding;
395                                 if (enc_switch.second > 0) {
396                                         os << '\n';
397                                         texrow.newline();
398                                 }
399                         }
400                         break;
401                 }
402         }
403
404         // In an inset with unlimited length (all in one row),
405         // don't allow any special options in the paragraph
406         if (!pit->forceDefaultParagraphs()) {
407                 if (pit->params().startOfAppendix()) {
408                         os << "\\appendix\n";
409                         texrow.newline();
410                 }
411
412                 if (!pit->params().spacing().isDefault()
413                         && (pit == paragraphs.begin()
414                             || !boost::prior(pit)->hasSameLayout(*pit)))
415                 {
416                         os << from_ascii(pit->params().spacing().writeEnvirBegin())
417                             << '\n';
418                         texrow.newline();
419                 }
420
421                 if (style->isCommand()) {
422                         os << '\n';
423                         texrow.newline();
424                 }
425         }
426
427         switch (style->latextype) {
428         case LATEX_COMMAND:
429                 os << '\\' << from_ascii(style->latexname());
430
431                 // Separate handling of optional argument inset.
432                 if (style->optionalargs > 0) {
433                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
434                                                     style->optionalargs);
435                         while (ret > 0) {
436                                 texrow.newline();
437                                 --ret;
438                         }
439                 }
440                 else
441                         os << from_ascii(style->latexparam());
442                 break;
443         case LATEX_ITEM_ENVIRONMENT:
444         case LATEX_LIST_ENVIRONMENT:
445                 os << "\\item ";
446                 break;
447         case LATEX_BIB_ENVIRONMENT:
448                 // ignore this, the inset will write itself
449                 break;
450         default:
451                 break;
452         }
453
454         Font const outerfont =
455                 outerFont(std::distance(paragraphs.begin(), pit),
456                           paragraphs);
457
458         // FIXME UNICODE
459         os << from_utf8(everypar);
460         bool need_par = pit->latex(buf, bparams, outerfont,
461                                              os, texrow, runparams);
462
463         // Make sure that \\par is done with the font of the last
464         // character if this has another size as the default.
465         // This is necessary because LaTeX (and LyX on the screen)
466         // calculates the space between the baselines according
467         // to this font. (Matthias)
468         //
469         // Is this really needed ? (Dekel)
470         // We do not need to use to change the font for the last paragraph
471         // or for a command.
472
473         Font const font =
474                 (pit->empty()
475                  ? pit->getLayoutFont(bparams, outerfont)
476                  : pit->getFont(bparams, pit->size() - 1, outerfont));
477
478         bool is_command = style->isCommand();
479
480         if (style->resfont.size() != font.fontInfo().size()
481             && boost::next(pit) != paragraphs.end()
482             && !is_command) {
483                 if (!need_par)
484                         os << '{';
485                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
486         } else if (need_par) {
487                 os << "\\par}";
488         } else if (is_command)
489                 os << '}';
490
491         bool pending_newline = false;
492         switch (style->latextype) {
493         case LATEX_ITEM_ENVIRONMENT:
494         case LATEX_LIST_ENVIRONMENT:
495                 if (boost::next(pit) != paragraphs.end()
496                     && (pit->params().depth() < boost::next(pit)->params().depth()))
497                         pending_newline = true;
498                 break;
499         case LATEX_ENVIRONMENT: {
500                 // if its the last paragraph of the current environment
501                 // skip it otherwise fall through
502                 ParagraphList::const_iterator next = boost::next(pit);
503
504                 if (next != paragraphs.end()
505                     && (next->layout() != pit->layout()
506                         || next->params().depth() != pit->params().depth()))
507                         break;
508         }
509
510                 // fall through possible
511         default:
512                 // we don't need it for the last paragraph!!!
513                 if (boost::next(pit) != paragraphs.end())
514                         pending_newline = true;
515         }
516
517         if (!pit->forceDefaultParagraphs()) {
518                 if (!pit->params().spacing().isDefault()
519                         && (boost::next(pit) == paragraphs.end()
520                             || !boost::next(pit)->hasSameLayout(*pit)))
521                 {
522                         if (pending_newline) {
523                                 os << '\n';
524                                 texrow.newline();
525                         }
526                         os << from_ascii(pit->params().spacing().writeEnvirEnd());
527                         pending_newline = true;
528                 }
529         }
530
531         // Closing the language is needed for the last paragraph; it is also
532         // needed if we're within an \L or \R that we may have opened above (not
533         // necessarily in this paragraph) and are about to close.
534         bool closing_rtl_ltr_environment = 
535                 // not for ArabTeX
536                 (par_language->lang() != "arabic_arabtex" &&
537                  outer_language->lang() != "arabic_arabtex") &&
538                 // have we opened and \L or \R environment?
539                 runparams.local_font != 0 &&
540                 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
541                 // are we about to close the language?
542                 ((boost::next(pit) != paragraphs.end() &&
543                   par_language->babel() != 
544                         (boost::next(pit)->getParLanguage(bparams))->babel()) ||
545                  (boost::next(pit) == paragraphs.end() &&
546                   par_language->babel() != outer_language->babel()));
547
548         if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
549             && par_language->babel() != outer_language->babel())) {
550                 // Since \selectlanguage write the language to the aux file,
551                 // we need to reset the language at the end of footnote or
552                 // float.
553
554                 if (pending_newline) {
555                         os << '\n';
556                         texrow.newline();
557                 }
558                 if (lyxrc.language_command_end.empty()) {
559                         if (!prev_language->babel().empty()) {
560                                 os << from_ascii(subst(
561                                         lyxrc.language_command_begin,
562                                         "$$lang",
563                                         prev_language->babel()));
564                                 pending_newline = true;
565                         }
566                 } else if (!par_language->babel().empty()) {
567                         os << from_ascii(subst(
568                                 lyxrc.language_command_end,
569                                 "$$lang",
570                                 par_language->babel()));
571                         pending_newline = true;
572                 }
573         }
574         if (closing_rtl_ltr_environment)
575                 os << "}";
576
577         if (pending_newline) {
578                 os << '\n';
579                 texrow.newline();
580         }
581
582         // If this is the last paragraph, and a local_font was set upon entering
583         // the inset, the encoding should be set back to that local_font's 
584         // encoding. We don't use switchEncoding(), because no explicit encoding
585         // switch command is needed, since latex will automatically revert to it
586         // when this inset closes.
587         // This switch is only necessary if we're using "auto" or "default" 
588         // encoding. 
589         if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
590                 runparams_in.encoding = runparams_in.local_font->language()->encoding();
591                 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
592                         os << setEncoding(runparams_in.encoding->iconvName());
593
594         }
595         // Otherwise, the current encoding should be set for the next paragraph.
596         else
597                 runparams_in.encoding = runparams.encoding;
598
599
600         // we don't need it for the last paragraph!!!
601         // Note from JMarc: we will re-add a \n explicitely in
602         // TeXEnvironment, because it is needed in this case
603         if (boost::next(pit) != paragraphs.end()) {
604                 os << '\n';
605                 texrow.newline();
606         }
607
608         if (boost::next(pit) != paragraphs.end()) {
609                 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
610         }
611
612         return ++pit;
613 }
614
615 } // anon namespace
616
617
618 // LaTeX all paragraphs
619 void latexParagraphs(Buffer const & buf,
620                      ParagraphList const & paragraphs,
621                      odocstream & os,
622                      TexRow & texrow,
623                      OutputParams const & runparams,
624                      string const & everypar)
625 {
626         bool was_title = false;
627         bool already_title = false;
628         TextClass const & tclass = buf.params().getTextClass();
629         ParagraphList::const_iterator par = paragraphs.begin();
630         ParagraphList::const_iterator endpar = paragraphs.end();
631
632         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
633         // if only part of the paragraphs will be outputed
634         if (runparams.par_begin !=  runparams.par_end) {
635                 par = boost::next(paragraphs.begin(), runparams.par_begin);
636                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
637                 // runparams will be passed to nested paragraphs, so
638                 // we have to reset the range parameters.
639                 const_cast<OutputParams&>(runparams).par_begin = 0;
640                 const_cast<OutputParams&>(runparams).par_end = 0;
641         }
642
643         // if only_body
644         while (par != endpar) {
645                 ParagraphList::const_iterator lastpar = par;
646                 // well we have to check if we are in an inset with unlimited
647                 // length (all in one row) if that is true then we don't allow
648                 // any special options in the paragraph and also we don't allow
649                 // any environment other than the default layout of the
650                 // text class to be valid!
651                 if (!par->forceDefaultParagraphs()) {
652                         LayoutPtr const & layout = par->layout();
653
654                         if (layout->intitle) {
655                                 if (already_title) {
656                                         lyxerr << "Error in latexParagraphs: You"
657                                                 " should not mix title layouts"
658                                                 " with normal ones." << endl;
659                                 } else if (!was_title) {
660                                         was_title = true;
661                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
662                                                 os << "\\begin{"
663                                                     << from_ascii(tclass.titlename())
664                                                     << "}\n";
665                                                 texrow.newline();
666                                         }
667                                 }
668                         } else if (was_title && !already_title) {
669                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
670                                         os << "\\end{" << from_ascii(tclass.titlename())
671                                             << "}\n";
672                                 }
673                                 else {
674                                         os << "\\" << from_ascii(tclass.titlename())
675                                             << "\n";
676                                 }
677                                 texrow.newline();
678                                 already_title = true;
679                                 was_title = false;
680                         }
681
682                         if (layout->is_environment) {
683                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
684                                                 runparams, everypar);
685                         } else if (layout->isEnvironment() ||
686                                    !par->params().leftIndent().zero()) {
687                                 par = TeXEnvironment(buf, paragraphs, par, os,
688                                                      texrow, runparams);
689                         } else {
690                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
691                                                 runparams, everypar);
692                         }
693                 } else {
694                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
695                                         runparams, everypar);
696                 }
697                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
698                         break;
699         }
700         // It might be that we only have a title in this document
701         if (was_title && !already_title) {
702                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
703                         os << "\\end{" << from_ascii(tclass.titlename())
704                             << "}\n";
705                 }
706                 else {
707                         os << "\\" << from_ascii(tclass.titlename())
708                             << "\n";
709                                 }
710                 texrow.newline();
711         }
712 }
713
714
715 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
716                    bool moving_arg, Encoding const & oldEnc,
717                    Encoding const & newEnc)
718 {
719         if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
720                 || moving_arg)
721                 return make_pair(false, 0);
722
723         // Do nothing if the encoding is unchanged.
724         if (oldEnc.name() == newEnc.name())
725                 return make_pair(false, 0);
726
727         // FIXME We ignore encoding switches from/to encodings that do
728         // neither support the inputenc package nor the CJK package here.
729         // This does of course only work in special cases (e.g. switch from
730         // tis620-0 to latin1, but the text in latin1 contains ASCII only),
731         // but it is the best we can do
732         if (oldEnc.package() == Encoding::none
733                 || newEnc.package() == Encoding::none)
734                 return make_pair(false, 0);
735
736         LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
737                 << oldEnc.name() << " to "
738                 << newEnc.name() << endl;
739         os << setEncoding(newEnc.iconvName());
740         if (bparams.inputenc == "default")
741                 return make_pair(true, 0);
742
743         docstring const inputenc(from_ascii(newEnc.latexName()));
744         switch (newEnc.package()) {
745                 case Encoding::none:
746                         // shouldn't ever reach here, see above
747                         return make_pair(true, 0);
748                 case Encoding::inputenc: {
749                         int count = inputenc.length();
750                         if (oldEnc.package() == Encoding::CJK) {
751                                 os << "\\end{CJK}";
752                                 count += 9;
753                         }
754                         os << "\\inputencoding{" << inputenc << '}';
755                         return make_pair(true, count + 16);
756                  }
757                 case Encoding::CJK: {
758                         int count = inputenc.length();
759                         if (oldEnc.package() == Encoding::CJK) {
760                                 os << "\\end{CJK}";
761                                 count += 9;
762                         }
763                         os << "\\begin{CJK}{" << inputenc << "}{}";
764                         return make_pair(true, count + 15);
765                 }
766         }
767         // Dead code to avoid a warning:
768         return make_pair(true, 0);
769 }
770
771 } // namespace lyx