]> git.lyx.org Git - lyx.git/blob - src/output_latex.cpp
8532b403fa7559736a7de6885869cf03059391fd
[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                 Font const outerfont =
258                         outerFont(std::distance(paragraphs.begin(), pit),
259                                   paragraphs);
260
261                 /*bool need_par = */ pit->latex(buf, bparams, outerfont,
262                         os, texrow, runparams_in);
263
264                 os << '\n';
265                 texrow.newline();
266                 return ++pit;
267         }
268
269         // In an inset with unlimited length (all in one row),
270         // force layout to default
271         if (!pit->forceDefaultParagraphs())
272                 style = pit->layout();
273         else
274                 style = bparams.getTextClass().defaultLayout();
275
276         OutputParams runparams = runparams_in;
277         runparams.moving_arg |= style->needprotect;
278
279         // This paragraph's language
280         Language const * const par_language = pit->getParLanguage(bparams);
281         // The document's language
282         Language const * const doc_language = bparams.language;
283         // The language that was in effect when the environemnt this paragraph is 
284         // inside of was opened
285         Language const * const outer_language = 
286                 (runparams.local_font != 0) ?
287                         runparams.local_font->language() : doc_language;
288         // The previous language that was in effect is either the language of
289         // the previous paragraph, if there is one, or else the outer language
290         // if there is no previous paragraph
291         Language const * const prev_language =
292                 (pit != paragraphs.begin()) ?
293                         boost::prior(pit)->getParLanguage(bparams) : outer_language;
294
295         if (par_language->babel() != prev_language->babel()
296             // check if we already put language command in TeXEnvironment()
297             && !(style->isEnvironment()
298                  && (pit == paragraphs.begin() ||
299                      (boost::prior(pit)->layout() != pit->layout() &&
300                       boost::prior(pit)->getDepth() <= pit->getDepth())
301                      || boost::prior(pit)->getDepth() < pit->getDepth())))
302         {
303                 if (!lyxrc.language_command_end.empty() &&
304                     prev_language->babel() != outer_language->babel() &&
305                     !prev_language->babel().empty())
306                 {
307                         os << from_ascii(subst(lyxrc.language_command_end,
308                                 "$$lang",
309                                 prev_language->babel()))
310                            << '\n';
311                         texrow.newline();
312                 }
313
314                 // We need to open a new language if we couldn't close the previous 
315                 // one (because there's no language_command_end); and even if we closed
316                 // the previous one, if the current language is different than the
317                 // outer_language (which is currently in effect once the previous one
318                 // is closed).
319                 if ((lyxrc.language_command_end.empty() ||
320                      par_language->babel() != outer_language->babel()) &&
321                     !par_language->babel().empty()) {
322                         // If we're inside an inset, and that inset is within an \L or \R
323                         // (or equivalents), then within the inset, too, any opposite
324                         // language paragraph should appear within an \L or \R (in addition
325                         // to, outside of, the normal language switch commands).
326                         // This behavior is not correct for ArabTeX, though.
327                         if (    // not for ArabTeX
328                                         (par_language->lang() != "arabic_arabtex" &&
329                                          outer_language->lang() != "arabic_arabtex") &&
330                                         // are we in an inset?
331                                         runparams.local_font != 0 &&
332                                         // is the inset within an \L or \R?
333                                         // 
334                                         // FIXME: currently, we don't check this; this means that
335                                         // we'll have unnnecessary \L and \R commands, but that 
336                                         // doesn't seem to hurt (though latex will complain)
337                                         // 
338                                         // is this paragraph in the opposite direction?
339                                         runparams.local_font->isRightToLeft() !=
340                                                 par_language->rightToLeft()
341                                 ) {
342                                 // FIXME: I don't have a working copy of the Arabi package, so
343                                 // I'm not sure if the farsi and arabic_arabi stuff is correct
344                                 // or not...
345                                 if (par_language->lang() == "farsi")
346                                         os << "\\textFR{";
347                                 else if (outer_language->lang() == "farsi")
348                                         os << "\\textLR{";
349                                 else if (par_language->lang() == "arabic_arabi")
350                                         os << "\\textAR{";
351                                 else if (outer_language->lang() == "arabic_arabi")
352                                         os << "\\textLR{";
353                                 // remaining RTL languages currently is hebrew
354                                 else if (par_language->rightToLeft())
355                                         os << "\\R{";
356                                 else
357                                         os << "\\L{";
358                         }
359                         os << from_ascii(subst(
360                                 lyxrc.language_command_begin,
361                                 "$$lang",
362                                 par_language->babel()))
363                            << '\n';
364                         texrow.newline();
365                 }
366         }
367
368         // Switch file encoding if necessary; no need to do this for "default"
369         // encoding, since this only affects the position of the outputted
370         // \inputencoding command; the encoding switch will occur when necessary
371         if (bparams.inputenc == "auto" &&
372             runparams.encoding->package() == Encoding::inputenc) {
373                 // Look ahead for future encoding changes.
374                 // We try to output them at the beginning of the paragraph,
375                 // since the \inputencoding command is not allowed e.g. in
376                 // sections.
377                 for (pos_type i = 0; i < pit->size(); ++i) {
378                         char_type const c = pit->getChar(i);
379                         if (c < 0x80)
380                                 continue;
381                         if (pit->isInset(i))
382                                 break;
383                         // All characters before c are in the ASCII range, and
384                         // c is non-ASCII (but no inset), so change the
385                         // encoding to that required by the language of c.
386                         Encoding const * const encoding =
387                                 pit->getFontSettings(bparams, i).language()->encoding();
388                         pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
389                                         *(runparams.encoding), *encoding);
390                         if (encoding->package() == Encoding::inputenc && enc_switch.first) {
391                                 runparams.encoding = encoding;
392                                 if (enc_switch.second > 0) {
393                                         os << '\n';
394                                         texrow.newline();
395                                 }
396                         }
397                         break;
398                 }
399         }
400
401         // In an inset with unlimited length (all in one row),
402         // don't allow any special options in the paragraph
403         if (!pit->forceDefaultParagraphs()) {
404                 if (pit->params().startOfAppendix()) {
405                         os << "\\appendix\n";
406                         texrow.newline();
407                 }
408
409                 if (!pit->params().spacing().isDefault()
410                         && (pit == paragraphs.begin()
411                             || !boost::prior(pit)->hasSameLayout(*pit)))
412                 {
413                         os << from_ascii(pit->params().spacing().writeEnvirBegin())
414                             << '\n';
415                         texrow.newline();
416                 }
417
418                 if (style->isCommand()) {
419                         os << '\n';
420                         texrow.newline();
421                 }
422         }
423
424         switch (style->latextype) {
425         case LATEX_COMMAND:
426                 os << '\\' << from_ascii(style->latexname());
427
428                 // Separate handling of optional argument inset.
429                 if (style->optionalargs > 0) {
430                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
431                                                     style->optionalargs);
432                         while (ret > 0) {
433                                 texrow.newline();
434                                 --ret;
435                         }
436                 }
437                 else
438                         os << from_ascii(style->latexparam());
439                 break;
440         case LATEX_ITEM_ENVIRONMENT:
441         case LATEX_LIST_ENVIRONMENT:
442                 os << "\\item ";
443                 break;
444         case LATEX_BIB_ENVIRONMENT:
445                 // ignore this, the inset will write itself
446                 break;
447         default:
448                 break;
449         }
450
451         Font const outerfont =
452                 outerFont(std::distance(paragraphs.begin(), pit),
453                           paragraphs);
454
455         // FIXME UNICODE
456         os << from_utf8(everypar);
457         bool need_par = pit->latex(buf, bparams, outerfont,
458                                              os, texrow, runparams);
459
460         // Make sure that \\par is done with the font of the last
461         // character if this has another size as the default.
462         // This is necessary because LaTeX (and LyX on the screen)
463         // calculates the space between the baselines according
464         // to this font. (Matthias)
465         //
466         // Is this really needed ? (Dekel)
467         // We do not need to use to change the font for the last paragraph
468         // or for a command.
469
470         Font const font =
471                 (pit->empty()
472                  ? pit->getLayoutFont(bparams, outerfont)
473                  : pit->getFont(bparams, pit->size() - 1, outerfont));
474
475         bool is_command = style->isCommand();
476
477         if (style->resfont.size() != font.fontInfo().size()
478             && boost::next(pit) != paragraphs.end()
479             && !is_command) {
480                 if (!need_par)
481                         os << '{';
482                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
483         } else if (need_par) {
484                 os << "\\par}";
485         } else if (is_command)
486                 os << '}';
487
488         bool pending_newline = false;
489         switch (style->latextype) {
490         case LATEX_ITEM_ENVIRONMENT:
491         case LATEX_LIST_ENVIRONMENT:
492                 if (boost::next(pit) != paragraphs.end()
493                     && (pit->params().depth() < boost::next(pit)->params().depth()))
494                         pending_newline = true;
495                 break;
496         case LATEX_ENVIRONMENT: {
497                 // if its the last paragraph of the current environment
498                 // skip it otherwise fall through
499                 ParagraphList::const_iterator next = boost::next(pit);
500
501                 if (next != paragraphs.end()
502                     && (next->layout() != pit->layout()
503                         || next->params().depth() != pit->params().depth()))
504                         break;
505         }
506
507                 // fall through possible
508         default:
509                 // we don't need it for the last paragraph!!!
510                 if (boost::next(pit) != paragraphs.end())
511                         pending_newline = true;
512         }
513
514         if (!pit->forceDefaultParagraphs()) {
515                 if (!pit->params().spacing().isDefault()
516                         && (boost::next(pit) == paragraphs.end()
517                             || !boost::next(pit)->hasSameLayout(*pit)))
518                 {
519                         if (pending_newline) {
520                                 os << '\n';
521                                 texrow.newline();
522                         }
523                         os << from_ascii(pit->params().spacing().writeEnvirEnd());
524                         pending_newline = true;
525                 }
526         }
527
528         // Closing the language is needed for the last paragraph; it is also
529         // needed if we're within an \L or \R that we may have opened above (not
530         // necessarily in this paragraph) and are about to close.
531         bool closing_rtl_ltr_environment = 
532                 // not for ArabTeX
533                 (par_language->lang() != "arabic_arabtex" &&
534                  outer_language->lang() != "arabic_arabtex") &&
535                 // have we opened and \L or \R environment?
536                 runparams.local_font != 0 &&
537                 runparams.local_font->isRightToLeft() != par_language->rightToLeft() &&
538                 // are we about to close the language?
539                 ((boost::next(pit) != paragraphs.end() &&
540                   par_language->babel() != 
541                         (boost::next(pit)->getParLanguage(bparams))->babel()) ||
542                  (boost::next(pit) == paragraphs.end() &&
543                   par_language->babel() != outer_language->babel()));
544
545         if (closing_rtl_ltr_environment || (boost::next(pit) == paragraphs.end()
546             && par_language->babel() != outer_language->babel())) {
547                 // Since \selectlanguage write the language to the aux file,
548                 // we need to reset the language at the end of footnote or
549                 // float.
550
551                 if (pending_newline) {
552                         os << '\n';
553                         texrow.newline();
554                 }
555                 if (lyxrc.language_command_end.empty()) {
556                         if (!prev_language->babel().empty()) {
557                                 os << from_ascii(subst(
558                                         lyxrc.language_command_begin,
559                                         "$$lang",
560                                         prev_language->babel()));
561                                 pending_newline = true;
562                         }
563                 } else if (!par_language->babel().empty()) {
564                         os << from_ascii(subst(
565                                 lyxrc.language_command_end,
566                                 "$$lang",
567                                 par_language->babel()));
568                         pending_newline = true;
569                 }
570         }
571         if (closing_rtl_ltr_environment)
572                 os << "}";
573
574         if (pending_newline) {
575                 os << '\n';
576                 texrow.newline();
577         }
578
579         // If this is the last paragraph, and a local_font was set upon entering
580         // the inset, the encoding should be set back to that local_font's 
581         // encoding. We don't use switchEncoding(), because no explicit encoding
582         // switch command is needed, since latex will automatically revert to it
583         // when this inset closes.
584         // This switch is only necessary if we're using "auto" or "default" 
585         // encoding. 
586         if (boost::next(pit) == paragraphs.end() && runparams_in.local_font != 0) {
587                 runparams_in.encoding = runparams_in.local_font->language()->encoding();
588                 if (bparams.inputenc == "auto" || bparams.inputenc == "default")
589                         os << setEncoding(runparams_in.encoding->iconvName());
590
591         }
592         // Otherwise, the current encoding should be set for the next paragraph.
593         else
594                 runparams_in.encoding = runparams.encoding;
595
596
597         // we don't need it for the last paragraph!!!
598         // Note from JMarc: we will re-add a \n explicitely in
599         // TeXEnvironment, because it is needed in this case
600         if (boost::next(pit) != paragraphs.end()) {
601                 os << '\n';
602                 texrow.newline();
603         }
604
605         if (boost::next(pit) != paragraphs.end()) {
606                 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
607         }
608
609         return ++pit;
610 }
611
612 } // anon namespace
613
614
615 // LaTeX all paragraphs
616 void latexParagraphs(Buffer const & buf,
617                      ParagraphList const & paragraphs,
618                      odocstream & os,
619                      TexRow & texrow,
620                      OutputParams const & runparams,
621                      string const & everypar)
622 {
623         bool was_title = false;
624         bool already_title = false;
625         TextClass const & tclass = buf.params().getTextClass();
626         ParagraphList::const_iterator par = paragraphs.begin();
627         ParagraphList::const_iterator endpar = paragraphs.end();
628
629         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
630         // if only part of the paragraphs will be outputed
631         if (runparams.par_begin !=  runparams.par_end) {
632                 par = boost::next(paragraphs.begin(), runparams.par_begin);
633                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
634                 // runparams will be passed to nested paragraphs, so
635                 // we have to reset the range parameters.
636                 const_cast<OutputParams&>(runparams).par_begin = 0;
637                 const_cast<OutputParams&>(runparams).par_end = 0;
638         }
639
640         // if only_body
641         while (par != endpar) {
642                 ParagraphList::const_iterator lastpar = par;
643                 // well we have to check if we are in an inset with unlimited
644                 // length (all in one row) if that is true then we don't allow
645                 // any special options in the paragraph and also we don't allow
646                 // any environment other than the default layout of the
647                 // text class to be valid!
648                 if (!par->forceDefaultParagraphs()) {
649                         LayoutPtr const & layout = par->layout();
650
651                         if (layout->intitle) {
652                                 if (already_title) {
653                                         lyxerr << "Error in latexParagraphs: You"
654                                                 " should not mix title layouts"
655                                                 " with normal ones." << endl;
656                                 } else if (!was_title) {
657                                         was_title = true;
658                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
659                                                 os << "\\begin{"
660                                                     << from_ascii(tclass.titlename())
661                                                     << "}\n";
662                                                 texrow.newline();
663                                         }
664                                 }
665                         } else if (was_title && !already_title) {
666                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
667                                         os << "\\end{" << from_ascii(tclass.titlename())
668                                             << "}\n";
669                                 }
670                                 else {
671                                         os << "\\" << from_ascii(tclass.titlename())
672                                             << "\n";
673                                 }
674                                 texrow.newline();
675                                 already_title = true;
676                                 was_title = false;
677                         }
678
679                         if (layout->is_environment) {
680                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
681                                                 runparams, everypar);
682                         } else if (layout->isEnvironment() ||
683                                    !par->params().leftIndent().zero()) {
684                                 par = TeXEnvironment(buf, paragraphs, par, os,
685                                                      texrow, runparams);
686                         } else {
687                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
688                                                 runparams, everypar);
689                         }
690                 } else {
691                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
692                                         runparams, everypar);
693                 }
694                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
695                         break;
696         }
697         // It might be that we only have a title in this document
698         if (was_title && !already_title) {
699                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
700                         os << "\\end{" << from_ascii(tclass.titlename())
701                             << "}\n";
702                 }
703                 else {
704                         os << "\\" << from_ascii(tclass.titlename())
705                             << "\n";
706                                 }
707                 texrow.newline();
708         }
709 }
710
711
712 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
713                    bool moving_arg, Encoding const & oldEnc,
714                    Encoding const & newEnc)
715 {
716         if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
717                 || moving_arg)
718                 return make_pair(false, 0);
719
720         // Do nothing if the encoding is unchanged.
721         if (oldEnc.name() == newEnc.name())
722                 return make_pair(false, 0);
723
724         // FIXME We ignore encoding switches from/to encodings that do
725         // neither support the inputenc package nor the CJK package here.
726         // This does of course only work in special cases (e.g. switch from
727         // tis620-0 to latin1, but the text in latin1 contains ASCII only),
728         // but it is the best we can do
729         if (oldEnc.package() == Encoding::none
730                 || newEnc.package() == Encoding::none)
731                 return make_pair(false, 0);
732
733         LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
734                 << oldEnc.name() << " to "
735                 << newEnc.name() << endl;
736         os << setEncoding(newEnc.iconvName());
737         if (bparams.inputenc == "default")
738                 return make_pair(true, 0);
739
740         docstring const inputenc(from_ascii(newEnc.latexName()));
741         switch (newEnc.package()) {
742                 case Encoding::none:
743                         // shouldn't ever reach here, see above
744                         return make_pair(true, 0);
745                 case Encoding::inputenc: {
746                         int count = inputenc.length();
747                         if (oldEnc.package() == Encoding::CJK) {
748                                 os << "\\end{CJK}";
749                                 count += 9;
750                         }
751                         os << "\\inputencoding{" << inputenc << '}';
752                         return make_pair(true, count + 16);
753                  }
754                 case Encoding::CJK: {
755                         int count = inputenc.length();
756                         if (oldEnc.package() == Encoding::CJK) {
757                                 os << "\\end{CJK}";
758                                 count += 9;
759                         }
760                         os << "\\begin{CJK}{" << inputenc << "}{}";
761                         return make_pair(true, count + 15);
762                 }
763         }
764         // Dead code to avoid a warning:
765         return make_pair(true, 0);
766 }
767
768 } // namespace lyx