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