]> git.lyx.org Git - lyx.git/blob - src/output_latex.cpp
Revert "Objective-C compililation support with cmake and C++11"
[lyx.git] / src / output_latex.cpp
1 /**
2  * \file output_latex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "output_latex.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "Encoding.h"
18 #include "Font.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 "ParagraphParameters.h"
26 #include "TextClass.h"
27 #include "TexRow.h"
28
29 #include "insets/InsetBibitem.h"
30 #include "insets/InsetArgument.h"
31
32 #include "support/lassert.h"
33 #include "support/convert.h"
34 #include "support/debug.h"
35 #include "support/lstrings.h"
36 #include "support/textutils.h"
37
38 #include <algorithm>
39 #include <boost/next_prior.hpp>
40 #include <list>
41
42 using namespace std;
43 using namespace lyx::support;
44
45
46 namespace lyx {
47
48 namespace {
49
50 enum OpenEncoding {
51         none,
52         inputenc,
53         CJK
54 };
55
56 // FIXME THREAD
57 // There could easily be a conflict here, with the export process
58 // setting this one way, and a View>Source process (possbily for
59 // another Buffer) resetting it.
60 static int open_encoding_ = none;
61 static int cjk_inherited_ = 0;
62 Language const * prev_env_language_ = 0;
63
64
65 string const getPolyglossiaEnvName(Language const * lang)
66 {
67         string result = lang->polyglossia();
68         if (result == "arabic")
69                 // exceptional spelling; see polyglossia docs.
70                 result = "Arabic";
71         return result;
72 }
73
74
75 struct TeXEnvironmentData
76 {
77         bool cjk_nested;
78         Layout const * style;
79         Language const * par_language;
80         Encoding const * prev_encoding;
81         bool leftindent_open;
82 };
83
84
85 static TeXEnvironmentData prepareEnvironment(Buffer const & buf,
86                                         Text const & text,
87                                         ParagraphList::const_iterator pit,
88                                         otexstream & os,
89                                         OutputParams const & runparams)
90 {
91         TeXEnvironmentData data;
92
93         BufferParams const & bparams = buf.params();
94
95         // FIXME This test should not be necessary.
96         // We should perhaps issue an error if it is.
97         Layout const & style = text.inset().forcePlainLayout() ?
98                 bparams.documentClass().plainLayout() : pit->layout();
99
100         ParagraphList const & paragraphs = text.paragraphs();
101         ParagraphList::const_iterator const priorpit =
102                 pit == paragraphs.begin() ? pit : boost::prior(pit);
103
104         bool const use_prev_env_language = prev_env_language_ != 0
105                         && priorpit->layout().isEnvironment()
106                         && (priorpit->getDepth() > pit->getDepth()
107                             || (priorpit->getDepth() == pit->getDepth()
108                                 && priorpit->layout() != pit->layout()));
109
110         data.prev_encoding = runparams.encoding;
111         data.par_language = pit->getParLanguage(bparams);
112         Language const * const doc_language = bparams.language;
113         Language const * const prev_par_language =
114                 (pit != paragraphs.begin())
115                 ? (use_prev_env_language ? prev_env_language_
116                                          : priorpit->getParLanguage(bparams))
117                 : doc_language;
118
119         bool const use_polyglossia = runparams.use_polyglossia;
120         string const par_lang = use_polyglossia ?
121                 getPolyglossiaEnvName(data.par_language) : data.par_language->babel();
122         string const prev_par_lang = use_polyglossia ?
123                 getPolyglossiaEnvName(prev_par_language) : prev_par_language->babel();
124         string const doc_lang = use_polyglossia ?
125                 getPolyglossiaEnvName(doc_language) : doc_language->babel();
126         string const lang_begin_command = use_polyglossia ?
127                 "\\begin{$$lang}" : lyxrc.language_command_begin;
128         string const lang_end_command = use_polyglossia ?
129                 "\\end{$$lang}" : lyxrc.language_command_end;
130
131         if (par_lang != prev_par_lang) {
132                 if (!lang_end_command.empty() &&
133                     prev_par_lang != doc_lang &&
134                     !prev_par_lang.empty()) {
135                         os << from_ascii(subst(
136                                 lang_end_command,
137                                 "$$lang",
138                                 prev_par_lang))
139                           // the '%' is necessary to prevent unwanted whitespace
140                           << "%\n";
141                 }
142
143                 if ((lang_end_command.empty() ||
144                     par_lang != doc_lang) &&
145                     !par_lang.empty()) {
146                         os << from_ascii(subst(
147                                 lang_begin_command,
148                                 "$$lang",
149                                 par_lang));
150                         if (use_polyglossia
151                             && !data.par_language->polyglossiaOpts().empty())
152                                         os << "["
153                                            << from_ascii(data.par_language->polyglossiaOpts())
154                                            << "]";
155                           // the '%' is necessary to prevent unwanted whitespace
156                         os << "%\n";
157                 }
158         }
159
160         data.leftindent_open = false;
161         if (!pit->params().leftIndent().zero()) {
162                 os << "\\begin{LyXParagraphLeftIndent}{"
163                    << from_ascii(pit->params().leftIndent().asLatexString())
164                    << "}\n";
165                 data.leftindent_open = true;
166         }
167
168         if (style.isEnvironment()) {
169                 os << "\\begin{" << from_ascii(style.latexname()) << '}';
170                 if (!style.latexargs().empty()) {
171                         OutputParams rp = runparams;
172                         rp.local_font = &pit->getFirstFontSettings(bparams);
173                         latexArgInsets(paragraphs, pit, os, rp, style.latexargs());
174                 }
175                 if (style.latextype == LATEX_LIST_ENVIRONMENT) {
176                         os << '{'
177                            << pit->params().labelWidthString()
178                            << "}\n";
179                 } else if (style.labeltype == LABEL_BIBLIO) {
180                         if (pit->params().labelWidthString().empty())
181                                 os << '{' << bibitemWidest(buf, runparams) << "}\n";
182                         else
183                                 os << '{'
184                                   << pit->params().labelWidthString()
185                                   << "}\n";
186                 } else
187                         os << from_ascii(style.latexparam()) << '\n';
188         }
189         data.style = &style;
190
191         // in multilingual environments, the CJK tags have to be nested properly
192         data.cjk_nested = false;
193         if (data.par_language->encoding()->package() == Encoding::CJK &&
194             open_encoding_ != CJK && pit->isMultiLingual(bparams)) {
195                 if (prev_par_language->encoding()->package() == Encoding::CJK)
196                         os << "\\begin{CJK}{" << from_ascii(data.par_language->encoding()->latexName())
197                            << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
198                 open_encoding_ = CJK;
199                 data.cjk_nested = true;
200         }
201         return data;
202 }
203
204
205 static void finishEnvironment(otexstream & os, OutputParams const & runparams,
206                                TeXEnvironmentData const & data)
207 {
208         if (open_encoding_ == CJK && data.cjk_nested) {
209                 // We need to close the encoding even if it does not change
210                 // to do correct environment nesting
211                 os << "\\end{CJK}\n";
212                 open_encoding_ = none;
213         }
214
215         if (data.style->isEnvironment()) {
216                 os << "\\end{" << from_ascii(data.style->latexname()) << "}\n";
217                 prev_env_language_ = data.par_language;
218                 if (runparams.encoding != data.prev_encoding) {
219                         runparams.encoding = data.prev_encoding;
220                         if (!runparams.isFullUnicode())
221                                 os << setEncoding(data.prev_encoding->iconvName());
222                 }
223         }
224
225         if (data.leftindent_open) {
226                 os << "\\end{LyXParagraphLeftIndent}\n";
227                 prev_env_language_ = data.par_language;
228                 if (runparams.encoding != data.prev_encoding) {
229                         runparams.encoding = data.prev_encoding;
230                         if (!runparams.isFullUnicode())
231                                 os << setEncoding(data.prev_encoding->iconvName());
232                 }
233         }
234 }
235
236
237 void TeXEnvironment(Buffer const & buf, Text const & text,
238                     OutputParams const & runparams,
239                     pit_type & pit, otexstream & os)
240 {
241         ParagraphList const & paragraphs = text.paragraphs();
242         ParagraphList::const_iterator par = paragraphs.constIterator(pit);
243         LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << pit);
244
245         Layout const & current_layout = par->layout();
246         depth_type const current_depth = par->params().depth();
247         Length const & current_left_indent = par->params().leftIndent();
248
249         // This is for debugging purpose at the end.
250         pit_type const par_begin = pit;
251         for (; pit < runparams.par_end; ++pit) {
252                 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
253
254                 // check first if this is an higher depth paragraph.
255                 bool go_out = (par->params().depth() < current_depth);
256                 if (par->params().depth() == current_depth) {
257                         // This environment is finished.
258                         go_out |= (par->layout() != current_layout);
259                         go_out |= (par->params().leftIndent() != current_left_indent);
260                 }
261                 if (go_out) {
262                         // nothing to do here, restore pit and go out.
263                         pit--;
264                         break;
265                 }
266
267                 if (par->layout() == current_layout
268                         && par->params().depth() == current_depth
269                         && par->params().leftIndent() == current_left_indent) {
270                         // We are still in the same environment so TeXOnePar and continue;
271                         TeXOnePar(buf, text, pit, os, runparams);
272                         continue;
273                 }
274
275                 // We are now in a deeper environment.
276                 // Either par->layout() != current_layout
277                 // Or     par->params().depth() > current_depth
278                 // Or     par->params().leftIndent() != current_left_indent)
279
280                 if (par->layout().isParagraph()) {
281                         // FIXME (Lgb): How to handle this?
282                         //&& !suffixIs(os, "\n\n")
283
284                         // (ARRae) There should be at least one '\n' already but we need there to
285                         // be two for Standard paragraphs that are depth-increment'ed to be
286                         // output correctly. However, tables can also be paragraphs so
287                         // don't adjust them.
288
289                         // FIXME (Lgb): Will it ever harm to have one '\n' too
290                         // many? i.e. that we sometimes will have
291                         // three in a row.
292                         os << '\n';
293                 }
294
295                 // FIXME This test should not be necessary.
296                 // We should perhaps issue an error if it is.
297                 bool const force_plain_layout = text.inset().forcePlainLayout();
298                 Layout const & style = force_plain_layout
299                         ? buf.params().documentClass().plainLayout()
300                         : par->layout();
301
302                 if (!style.isEnvironment()) {
303                         // This is a standard paragraph, no need to call TeXEnvironment.
304                         TeXOnePar(buf, text, pit, os, runparams);
305                         continue;
306                 }
307
308                 // This is a new environment.
309                 TeXEnvironmentData const data =
310                         prepareEnvironment(buf, text, par, os, runparams);
311                 // Recursive call to TeXEnvironment!
312                 TeXEnvironment(buf, text, runparams, pit, os);
313                 finishEnvironment(os, runparams, data);
314         }
315
316         if (pit != runparams.par_end)
317                 LYXERR(Debug::LATEX, "TeXEnvironment for paragraph " << par_begin << " done.");
318 }
319
320
321 void getArgInsets(otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
322                   map<int, lyx::InsetArgument const *> ilist, vector<string> required, string const & prefix)
323 {
324         unsigned int const argnr = latexargs.size();
325         if (argnr == 0)
326                 return;
327
328         for (unsigned int i = 1; i <= argnr; ++i) {
329                 map<int, InsetArgument const *>::const_iterator lit = ilist.find(i);
330                 bool inserted = false;
331                 if (lit != ilist.end()) {
332                         InsetArgument const * ins = (*lit).second;
333                         if (ins) {
334                                 Layout::LaTeXArgMap::const_iterator const lait =
335                                                 latexargs.find(ins->name());
336                                 if (lait != latexargs.end()) {
337                                         Layout::latexarg arg = (*lait).second;
338                                         docstring ldelim = arg.mandatory ?
339                                                         from_ascii("{") : from_ascii("[");
340                                         docstring rdelim = arg.mandatory ?
341                                                         from_ascii("}") : from_ascii("]");
342                                         if (!arg.ldelim.empty())
343                                                 ldelim = arg.ldelim;
344                                         if (!arg.rdelim.empty())
345                                                 rdelim = arg.rdelim;
346                                         ins->latexArgument(os, runparams, ldelim, rdelim, arg.presetarg);
347                                         inserted = true;
348                                 }
349                         }
350                 }
351                 if (!inserted) {
352                         Layout::LaTeXArgMap::const_iterator lait = latexargs.begin();
353                         Layout::LaTeXArgMap::const_iterator const laend = latexargs.end();
354                         for (; lait != laend; ++lait) {
355                                 string const name = prefix + convert<string>(i);
356                                 if ((*lait).first == name) {
357                                         Layout::latexarg arg = (*lait).second;
358                                         docstring preset = arg.presetarg;
359                                         if (!arg.defaultarg.empty()) {
360                                                 if (!preset.empty())
361                                                         preset += ",";
362                                                 preset += arg.defaultarg;
363                                         }
364                                         if (arg.mandatory) {
365                                                 docstring ldelim = arg.ldelim.empty() ?
366                                                                 from_ascii("{") : arg.ldelim;
367                                                 docstring rdelim = arg.rdelim.empty() ?
368                                                                 from_ascii("}") : arg.rdelim;
369                                                 os << ldelim << preset << rdelim;
370                                         } else if (!preset.empty()) {
371                                                 docstring ldelim = arg.ldelim.empty() ?
372                                                                 from_ascii("[") : arg.ldelim;
373                                                 docstring rdelim = arg.rdelim.empty() ?
374                                                                 from_ascii("]") : arg.rdelim;
375                                                 os << ldelim << preset << rdelim;
376                                         } else if (find(required.begin(), required.end(),
377                                                    (*lait).first) != required.end()) {
378                                                 docstring ldelim = arg.ldelim.empty() ?
379                                                                 from_ascii("[") : arg.ldelim;
380                                                 docstring rdelim = arg.rdelim.empty() ?
381                                                                 from_ascii("]") : arg.rdelim;
382                                                 os << ldelim << rdelim;
383                                         } else
384                                                 break;
385                                 }
386                         }
387                 }
388         }
389 }
390
391
392 } // namespace anon
393
394
395 void latexArgInsets(Paragraph const & par, otexstream & os,
396         OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs, string const & prefix)
397 {
398         map<int, InsetArgument const *> ilist;
399         vector<string> required;
400
401         InsetList::const_iterator it = par.insetList().begin();
402         InsetList::const_iterator end = par.insetList().end();
403         for (; it != end; ++it) {
404                 if (it->inset->lyxCode() == ARG_CODE) {
405                         InsetArgument const * ins =
406                                 static_cast<InsetArgument const *>(it->inset);
407                         if (ins->name().empty())
408                                 LYXERR0("Error: Unnamed argument inset!");
409                         else {
410                                 string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
411                                 unsigned int const nr = convert<unsigned int>(name);
412                                 ilist[nr] = ins;
413                                 Layout::LaTeXArgMap::const_iterator const lit =
414                                                 latexargs.find(ins->name());
415                                 if (lit != latexargs.end()) {
416                                         Layout::latexarg const & arg = (*lit).second;
417                                         if (!arg.requires.empty()) {
418                                                 vector<string> req = getVectorFromString(arg.requires);
419                                                 required.insert(required.end(), req.begin(), req.end());
420                                         }
421                                 }
422                         }
423                 }
424         }
425         getArgInsets(os, runparams, latexargs, ilist, required, prefix);
426 }
427
428
429 void latexArgInsets(ParagraphList const & pars, ParagraphList::const_iterator pit,
430         otexstream & os, OutputParams const & runparams, Layout::LaTeXArgMap const & latexargs,
431         string const & prefix)
432 {
433         map<int, InsetArgument const *> ilist;
434         vector<string> required;
435
436         depth_type const current_depth = pit->params().depth();
437         Layout const current_layout = pit->layout();
438
439         // get the first paragraph in sequence with this layout and depth
440         pit_type offset = 0;
441         while (true) {
442                 if (boost::prior(pit, offset) == pars.begin())
443                         break;
444                 ParagraphList::const_iterator priorpit = boost::prior(pit, offset + 1);
445                 if (priorpit->layout() == current_layout
446                     && priorpit->params().depth() == current_depth)
447                         ++offset;
448                 else
449                         break;
450         }
451
452         ParagraphList::const_iterator spit = boost::prior(pit, offset);
453
454         for (; spit != pars.end(); ++spit) {
455                 if (spit->layout() != current_layout || spit->params().depth() < current_depth)
456                         break;
457                 if (spit->params().depth() > current_depth)
458                         continue;
459                 InsetList::const_iterator it = spit->insetList().begin();
460                 InsetList::const_iterator end = spit->insetList().end();
461                 for (; it != end; ++it) {
462                         if (it->inset->lyxCode() == ARG_CODE) {
463                                 InsetArgument const * ins =
464                                         static_cast<InsetArgument const *>(it->inset);
465                                 if (ins->name().empty())
466                                         LYXERR0("Error: Unnamed argument inset!");
467                                 else {
468                                         string const name = prefix.empty() ? ins->name() : split(ins->name(), ':');
469                                         unsigned int const nr = convert<unsigned int>(name);
470                                         if (ilist.find(nr) == ilist.end())
471                                                 ilist[nr] = ins;
472                                         Layout::LaTeXArgMap::const_iterator const lit =
473                                                         latexargs.find(ins->name());
474                                         if (lit != latexargs.end()) {
475                                                 Layout::latexarg const & arg = (*lit).second;
476                                                 if (!arg.requires.empty()) {
477                                                         vector<string> req = getVectorFromString(arg.requires);
478                                                         required.insert(required.end(), req.begin(), req.end());
479                                                 }
480                                         }
481                                 }
482                         }
483                 }
484         }
485         getArgInsets(os, runparams, latexargs, ilist, required, prefix);
486 }
487
488 namespace {
489
490 // output the proper paragraph start according to latextype.
491 void parStartCommand(Paragraph const & par, otexstream & os,
492                      OutputParams const & runparams, Layout const & style) 
493 {
494         switch (style.latextype) {
495         case LATEX_COMMAND:
496                 os << '\\' << from_ascii(style.latexname());
497
498                 // Command arguments
499                 if (!style.latexargs().empty())
500                         latexArgInsets(par, os, runparams, style.latexargs());
501                 os << from_ascii(style.latexparam());
502                 break;
503         case LATEX_ITEM_ENVIRONMENT:
504         case LATEX_LIST_ENVIRONMENT:
505                 os << "\\" + style.itemcommand();
506                 // Item arguments
507                 if (!style.itemargs().empty())
508                         latexArgInsets(par, os, runparams, style.itemargs(), "item:");
509                 os << " ";
510                 break;
511         case LATEX_BIB_ENVIRONMENT:
512                 // ignore this, the inset will write itself
513                 break;
514         default:
515                 break;
516         }
517 }
518
519 } // namespace anon
520
521 // FIXME: this should be anonymous
522 void TeXOnePar(Buffer const & buf,
523                Text const & text,
524                pit_type pit,
525                otexstream & os,
526                OutputParams const & runparams_in,
527                string const & everypar,
528                int start_pos, int end_pos)
529 {
530         BufferParams const & bparams = runparams_in.is_child
531                 ? buf.masterParams() : buf.params();
532         ParagraphList const & paragraphs = text.paragraphs();
533         Paragraph const & par = paragraphs.at(pit);
534         // FIXME This check should not really be needed.
535         // Perhaps we should issue an error if it is.
536         Layout const style = text.inset().forcePlainLayout() ?
537                 bparams.documentClass().plainLayout() : par.layout();
538
539         if (style.inpreamble)
540                 return;
541
542         LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " ptr " << &par << " '"
543                 << everypar << "'");
544
545         OutputParams runparams = runparams_in;
546         runparams.isLastPar = (pit == pit_type(paragraphs.size() - 1));
547         // We reinitialze par begin and end to be on the safe side
548         // with embedded inset as we don't know if they set those
549         // value correctly.
550         runparams.par_begin = 0;
551         runparams.par_end = 0;
552
553         bool const maintext = text.isMainText();
554         // we are at the beginning of an inset and CJK is already open;
555         // we count inheritation levels to get the inset nesting right.
556         if (pit == 0 && !maintext
557             && (cjk_inherited_ > 0 || open_encoding_ == CJK)) {
558                 cjk_inherited_ += 1;
559                 open_encoding_ = none;
560         }
561
562         if (text.inset().isPassThru()) {
563                 Font const outerfont = text.outerFont(pit);
564
565                 // No newline before first paragraph in this lyxtext
566                 if (pit > 0) {
567                         os << '\n';
568                         if (!text.inset().getLayout().parbreakIsNewline())
569                                 os << '\n';
570                 }
571
572                 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
573                 return;
574         }
575
576         Paragraph const * nextpar = runparams.isLastPar
577                 ? 0 : &paragraphs.at(pit + 1);
578
579         if (style.pass_thru) {
580                 Font const outerfont = text.outerFont(pit);
581                 parStartCommand(par, os, runparams, style);
582
583                 par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
584
585                 // I did not create a parEndCommand for this minuscule
586                 // task because in the other user of parStartCommand
587                 // the code is different (JMarc)
588                 if (style.isCommand())
589                         os << "}\n";
590                 else
591                         os << '\n';
592                 if (!style.parbreak_is_newline) {
593                         os << '\n';
594                 } else if (nextpar && !style.isEnvironment()) {
595                         Layout const nextstyle = text.inset().forcePlainLayout()
596                                 ? bparams.documentClass().plainLayout()
597                                 : nextpar->layout();
598                         if (nextstyle.name() != style.name())
599                                 os << '\n';
600                 }
601
602                 return;
603         }
604
605         // This paragraph's language
606         Language const * const par_language = par.getParLanguage(bparams);
607         // The document's language
608         Language const * const doc_language = bparams.language;
609         // The language that was in effect when the environment this paragraph is
610         // inside of was opened
611         Language const * const outer_language =
612                 (runparams.local_font != 0) ?
613                         runparams.local_font->language() : doc_language;
614
615         Paragraph const * priorpar = (pit == 0) ? 0 : &paragraphs.at(pit - 1);
616
617         // The previous language that was in effect is the language of the
618         // previous paragraph, unless the previous paragraph is inside an
619         // environment with nesting depth greater than (or equal to, but with
620         // a different layout) the current one. If there is no previous
621         // paragraph, the previous language is the outer language.
622         bool const use_prev_env_language = prev_env_language_ != 0
623                         && priorpar
624                         && priorpar->layout().isEnvironment()
625                         && (priorpar->getDepth() > par.getDepth()
626                             || (priorpar->getDepth() == par.getDepth()
627                                     && priorpar->layout() != par.layout()));
628         Language const * const prev_language =
629                 (pit != 0)
630                 ? (use_prev_env_language ? prev_env_language_
631                                          : priorpar->getParLanguage(bparams))
632                 : outer_language;
633
634
635         bool const use_polyglossia = runparams.use_polyglossia;
636         string const par_lang = use_polyglossia ?
637                 getPolyglossiaEnvName(par_language): par_language->babel();
638         string const prev_lang = use_polyglossia ?
639                 getPolyglossiaEnvName(prev_language) : prev_language->babel();
640         string const doc_lang = use_polyglossia ?
641                 getPolyglossiaEnvName(doc_language) : doc_language->babel();
642         string const outer_lang = use_polyglossia ?
643                 getPolyglossiaEnvName(outer_language) : outer_language->babel();
644         string lang_begin_command = use_polyglossia ?
645                 "\\begin{$$lang}" : lyxrc.language_command_begin;
646         string lang_end_command = use_polyglossia ?
647                 "\\end{$$lang}" : lyxrc.language_command_end;
648         // the '%' is necessary to prevent unwanted whitespace
649         string lang_command_termination = "%\n";
650
651         // In some insets (such as Arguments), we cannot use \selectlanguage
652         bool const localswitch = !use_polyglossia
653                 && text.inset().getLayout().forcelocalfontswitch();
654         if (localswitch) {
655                 lang_begin_command = lyxrc.language_command_local;
656                 lang_end_command = "}";
657                 lang_command_termination.clear();
658         }
659
660         if (par_lang != prev_lang
661                 // check if we already put language command in TeXEnvironment()
662                 && !(style.isEnvironment()
663                      && (pit == 0 || (priorpar->layout() != par.layout()
664                                           && priorpar->getDepth() <= par.getDepth())
665                                   || priorpar->getDepth() < par.getDepth())))
666         {
667                 if (!lang_end_command.empty() &&
668                     prev_lang != outer_lang &&
669                     !prev_lang.empty())
670                 {
671                         os << from_ascii(subst(lang_end_command,
672                                 "$$lang",
673                                 prev_lang))
674                            << lang_command_termination;
675                 }
676
677                 // We need to open a new language if we couldn't close the previous
678                 // one (because there's no language_command_end); and even if we closed
679                 // the previous one, if the current language is different than the
680                 // outer_language (which is currently in effect once the previous one
681                 // is closed).
682                 if ((lang_end_command.empty() || par_lang != outer_lang)
683                         && !par_lang.empty()) {
684                         // If we're inside an inset, and that inset is within an \L or \R
685                         // (or equivalents), then within the inset, too, any opposite
686                         // language paragraph should appear within an \L or \R (in addition
687                         // to, outside of, the normal language switch commands).
688                         // This behavior is not correct for ArabTeX, though.
689                         if (!use_polyglossia
690                             // not for ArabTeX
691                                 && par_language->lang() != "arabic_arabtex"
692                                 && outer_language->lang() != "arabic_arabtex"
693                             // are we in an inset?
694                             && runparams.local_font != 0
695                             // is the inset within an \L or \R?
696                             //
697                             // FIXME: currently, we don't check this; this means that
698                             // we'll have unnnecessary \L and \R commands, but that
699                             // doesn't seem to hurt (though latex will complain)
700                             //
701                             // is this paragraph in the opposite direction?
702                             && runparams.local_font->isRightToLeft() != par_language->rightToLeft()) {
703                                 // FIXME: I don't have a working copy of the Arabi package, so
704                                 // I'm not sure if the farsi and arabic_arabi stuff is correct
705                                 // or not...
706                                 if (par_language->lang() == "farsi")
707                                         os << "\\textFR{";
708                                 else if (outer_language->lang() == "farsi")
709                                         os << "\\textLR{";
710                                 else if (par_language->lang() == "arabic_arabi")
711                                         os << "\\textAR{";
712                                 else if (outer_language->lang() == "arabic_arabi")
713                                         os << "\\textLR{";
714                                 // remaining RTL languages currently is hebrew
715                                 else if (par_language->rightToLeft())
716                                         os << "\\R{";
717                                 else
718                                         os << "\\L{";
719                         }
720                         // With CJK, the CJK tag has to be closed first (see below)
721                         if (runparams.encoding->package() != Encoding::CJK
722                             && !par_lang.empty()) {
723                                 os << from_ascii(subst(
724                                         lang_begin_command,
725                                         "$$lang",
726                                         par_lang));
727                                 if (use_polyglossia
728                                     && !par_language->polyglossiaOpts().empty())
729                                                 os << "["
730                                                   << from_ascii(par_language->polyglossiaOpts())
731                                                   << "]";
732                                 os << lang_command_termination;
733                         }
734                 }
735         }
736
737         // Switch file encoding if necessary; no need to do this for "default"
738         // encoding, since this only affects the position of the outputted
739         // \inputencoding command; the encoding switch will occur when necessary
740         if (bparams.inputenc == "auto"
741                 && runparams.encoding->package() != Encoding::none) {
742                 // Look ahead for future encoding changes.
743                 // We try to output them at the beginning of the paragraph,
744                 // since the \inputencoding command is not allowed e.g. in
745                 // sections. For this reason we only set runparams.moving_arg
746                 // after checking for the encoding change, otherwise the
747                 // change would be always avoided by switchEncoding().
748                 for (pos_type i = 0; i < par.size(); ++i) {
749                         char_type const c = par.getChar(i);
750                         Encoding const * const encoding =
751                                 par.getFontSettings(bparams, i).language()->encoding();
752                         if (encoding->package() != Encoding::CJK
753                                 && runparams.encoding->package() == Encoding::inputenc
754                                 && isASCII(c))
755                                 continue;
756                         if (par.isInset(i))
757                                 break;
758                         // All characters before c are in the ASCII range, and
759                         // c is non-ASCII (but no inset), so change the
760                         // encoding to that required by the language of c.
761                         // With CJK, only add switch if we have CJK content at the beginning
762                         // of the paragraph
763                         if (i != 0 && encoding->package() == Encoding::CJK)
764                                 continue;
765
766                         pair<bool, int> enc_switch = switchEncoding(os.os(),
767                                                 bparams, runparams, *encoding);
768                         // the following is necessary after a CJK environment in a multilingual
769                         // context (nesting issue).
770                         if (par_language->encoding()->package() == Encoding::CJK
771                                 && open_encoding_ != CJK && cjk_inherited_ == 0) {
772                                 os << "\\begin{CJK}{" << from_ascii(par_language->encoding()->latexName())
773                                    << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
774                                 open_encoding_ = CJK;
775                         }
776                         if (encoding->package() != Encoding::none && enc_switch.first) {
777                                 if (enc_switch.second > 0) {
778                                         // the '%' is necessary to prevent unwanted whitespace
779                                         os << "%\n";
780                                 }
781                                 // With CJK, the CJK tag had to be closed first (see above)
782                                 if (runparams.encoding->package() == Encoding::CJK
783                                     && !par_lang.empty()) {
784                                         os << from_ascii(subst(
785                                                 lang_begin_command,
786                                                 "$$lang",
787                                                 par_lang))
788                                         << lang_command_termination;
789                                 }
790                                 runparams.encoding = encoding;
791                         }
792                         break;
793                 }
794         }
795
796         runparams.moving_arg |= style.needprotect;
797         Encoding const * const prev_encoding = runparams.encoding;
798
799         bool const useSetSpace = bparams.documentClass().provides("SetSpace");
800         if (par.allowParagraphCustomization()) {
801                 if (par.params().startOfAppendix()) {
802                         os << "\\appendix\n";
803                 }
804
805                 if (!par.params().spacing().isDefault()
806                         && (pit == 0 || !priorpar->hasSameLayout(par)))
807                 {
808                         os << from_ascii(par.params().spacing().writeEnvirBegin(useSetSpace))
809                             << '\n';
810                 }
811
812                 if (style.isCommand()) {
813                         os << '\n';
814                 }
815         }
816
817         parStartCommand(par, os, runparams, style);
818         Font const outerfont = text.outerFont(pit);
819
820         // FIXME UNICODE
821         os << from_utf8(everypar);
822         par.latex(bparams, outerfont, os, runparams, start_pos, end_pos);
823
824         // Make sure that \\par is done with the font of the last
825         // character if this has another size as the default.
826         // This is necessary because LaTeX (and LyX on the screen)
827         // calculates the space between the baselines according
828         // to this font. (Matthias)
829         //
830         // Is this really needed ? (Dekel)
831         // We do not need to use to change the font for the last paragraph
832         // or for a command.
833
834         Font const font = par.empty()
835                  ? par.getLayoutFont(bparams, outerfont)
836                  : par.getFont(bparams, par.size() - 1, outerfont);
837
838         bool const is_command = style.isCommand();
839
840         if (style.resfont.size() != font.fontInfo().size()
841             && nextpar
842             && !is_command) {
843                 os << '{';
844                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
845         } else if (is_command) {
846                 os << '}';
847                 if (!style.postcommandargs().empty())
848                         latexArgInsets(par, os, runparams, style.postcommandargs(), "post:");
849                 if (runparams.encoding != prev_encoding) {
850                         runparams.encoding = prev_encoding;
851                         if (!runparams.isFullUnicode())
852                                 os << setEncoding(prev_encoding->iconvName());
853                 }
854         }
855
856         bool pending_newline = false;
857         bool unskip_newline = false;
858         switch (style.latextype) {
859         case LATEX_ITEM_ENVIRONMENT:
860         case LATEX_LIST_ENVIRONMENT:
861                 if (nextpar && (par.params().depth() < nextpar->params().depth()))
862                         pending_newline = true;
863                 break;
864         case LATEX_ENVIRONMENT: {
865                 // if its the last paragraph of the current environment
866                 // skip it otherwise fall through
867                 if (nextpar
868                         && (nextpar->layout() != par.layout()
869                         || nextpar->params().depth() != par.params().depth()))
870                         break;
871         }
872
873         // fall through possible
874         default:
875                 // we don't need it for the last paragraph!!!
876                 if (nextpar)
877                         pending_newline = true;
878         }
879
880         if (par.allowParagraphCustomization()) {
881                 if (!par.params().spacing().isDefault()
882                         && (runparams.isLastPar || !nextpar->hasSameLayout(par))) {
883                         if (pending_newline)
884                                 os << '\n';
885                         os << from_ascii(par.params().spacing().writeEnvirEnd(useSetSpace));
886                         pending_newline = true;
887                 }
888         }
889
890         // Closing the language is needed for the last paragraph; it is also
891         // needed if we're within an \L or \R that we may have opened above (not
892         // necessarily in this paragraph) and are about to close.
893         bool closing_rtl_ltr_environment = !use_polyglossia
894                 // not for ArabTeX
895                 && (par_language->lang() != "arabic_arabtex"
896                     && outer_language->lang() != "arabic_arabtex")
897                 // have we opened an \L or \R environment?
898                 && runparams.local_font != 0
899                 && runparams.local_font->isRightToLeft() != par_language->rightToLeft()
900                 // are we about to close the language?
901                 &&((nextpar && par_language->babel() != (nextpar->getParLanguage(bparams))->babel())
902                    || (runparams.isLastPar && par_language->babel() != outer_language->babel()));
903
904         if (closing_rtl_ltr_environment
905             || (runparams.isLastPar
906                 && ((!use_polyglossia && par_language->babel() != outer_language->babel())
907                     || (use_polyglossia && par_language->polyglossia() != outer_language->polyglossia())))) {
908                 // Since \selectlanguage write the language to the aux file,
909                 // we need to reset the language at the end of footnote or
910                 // float.
911
912                 if (pending_newline)
913                         os << '\n';
914
915                 // when the paragraph uses CJK, the language has to be closed earlier
916                 if (font.language()->encoding()->package() != Encoding::CJK) {
917                         if (lang_end_command.empty()) {
918                                 // If this is a child, we should restore the
919                                 // master language after the last paragraph.
920                                 Language const * const current_language =
921                                         (runparams.isLastPar && runparams.master_language)
922                                                 ? runparams.master_language
923                                                 : outer_language;
924                                 string const current_lang = use_polyglossia
925                                         ? getPolyglossiaEnvName(current_language)
926                                         : current_language->babel();
927                                 if (!current_lang.empty()) {
928                                         os << from_ascii(subst(
929                                                 lang_begin_command,
930                                                 "$$lang",
931                                                 current_lang));
932                                         pending_newline = !localswitch;
933                                         unskip_newline = !localswitch;
934                                 }
935                         } else if (!par_lang.empty()) {
936                                 os << from_ascii(subst(
937                                         lang_end_command,
938                                         "$$lang",
939                                         par_lang));
940                                 pending_newline = !localswitch;
941                                 unskip_newline = !localswitch;
942                         }
943                 }
944         }
945         if (closing_rtl_ltr_environment)
946                 os << "}";
947
948         if (pending_newline) {
949                 if (unskip_newline)
950                         // prevent unwanted whitespace
951                         os << '%';
952                 os << '\n';
953         }
954
955         // if this is a CJK-paragraph and the next isn't, close CJK
956         // also if the next paragraph is a multilingual environment (because of nesting)
957         if (nextpar
958                 && open_encoding_ == CJK
959                 && (nextpar->getParLanguage(bparams)->encoding()->package() != Encoding::CJK
960                    || (nextpar->layout().isEnvironment() && nextpar->isMultiLingual(bparams)))
961                 // inbetween environments, CJK has to be closed later (nesting!)
962                 && (!style.isEnvironment() || !nextpar->layout().isEnvironment())) {
963                 os << "\\end{CJK}\n";
964                 open_encoding_ = none;
965         }
966
967         // If this is the last paragraph, close the CJK environment
968         // if necessary. If it's an environment, we'll have to \end that first.
969         if (runparams.isLastPar && !style.isEnvironment()) {
970                 switch (open_encoding_) {
971                         case CJK: {
972                                 // do nothing at the end of child documents
973                                 if (maintext && buf.masterBuffer() != &buf)
974                                         break;
975                                 // end of main text
976                                 if (maintext) {
977                                         os << "\n\\end{CJK}\n";
978                                 // end of an inset
979                                 } else
980                                         os << "\\end{CJK}";
981                                 open_encoding_ = none;
982                                 break;
983                         }
984                         case inputenc: {
985                                 os << "\\egroup";
986                                 open_encoding_ = none;
987                                 break;
988                         }
989                         case none:
990                         default:
991                                 // do nothing
992                                 break;
993                 }
994         }
995
996         // If this is the last paragraph, and a local_font was set upon entering
997         // the inset, and we're using "auto" or "default" encoding, the encoding
998         // should be set back to that local_font's encoding.
999         // However, do not change the encoding when a fully unicode aware backend
1000         // such as XeTeX is used.
1001         if (runparams.isLastPar && runparams_in.local_font != 0
1002             && runparams_in.encoding != runparams_in.local_font->language()->encoding()
1003             && (bparams.inputenc == "auto" || bparams.inputenc == "default")
1004             && (!runparams.isFullUnicode())) {
1005                 runparams_in.encoding = runparams_in.local_font->language()->encoding();
1006                 os << setEncoding(runparams_in.encoding->iconvName());
1007         }
1008         // Otherwise, the current encoding should be set for the next paragraph.
1009         else
1010                 runparams_in.encoding = runparams.encoding;
1011
1012
1013         // we don't need a newline for the last paragraph!!!
1014         // Note from JMarc: we will re-add a \n explicitly in
1015         // TeXEnvironment, because it is needed in this case
1016         if (nextpar) {
1017                 Layout const & next_layout = nextpar->layout();
1018                 if (style == next_layout
1019                     // no blank lines before environments!
1020                     || !next_layout.isEnvironment()
1021                     // unless there's a depth change
1022                     // FIXME What we really want to do here is put every \begin and \end
1023                     // tag on a new line (which was not the case with nested environments).
1024                     // But in the present state of play, we don't have access to the
1025                     // information whether the current TeX row is empty or not.
1026                     // For some ideas about how to fix this, see this thread:
1027                     // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg145787.html
1028                     || nextpar->params().depth() != par.params().depth()) {
1029                         os << '\n';
1030                 }
1031         }
1032
1033         LYXERR(Debug::LATEX, "TeXOnePar for paragraph " << pit << " done; ptr "
1034                 << &par << " next " << nextpar);
1035
1036         return;
1037 }
1038
1039
1040 // LaTeX all paragraphs
1041 void latexParagraphs(Buffer const & buf,
1042                      Text const & text,
1043                      otexstream & os,
1044                      OutputParams const & runparams,
1045                      string const & everypar)
1046 {
1047         LASSERT(runparams.par_begin <= runparams.par_end,
1048                 { os << "% LaTeX Output Error\n"; return; } );
1049
1050         BufferParams const & bparams = buf.params();
1051
1052         bool const maintext = text.isMainText();
1053         bool const is_child = buf.masterBuffer() != &buf;
1054
1055         // Open a CJK environment at the beginning of the main buffer
1056         // if the document's language is a CJK language
1057         // (but not in child documents)
1058         if (maintext && !is_child
1059             && bparams.encoding().package() == Encoding::CJK) {
1060                 os << "\\begin{CJK}{" << from_ascii(bparams.encoding().latexName())
1061                 << "}{" << from_ascii(bparams.fonts_cjk) << "}%\n";
1062                 open_encoding_ = CJK;
1063         }
1064         // if "auto begin" is switched off, explicitly switch the
1065         // language on at start
1066         string const mainlang = runparams.use_polyglossia
1067                 ? getPolyglossiaEnvName(bparams.language)
1068                 : bparams.language->babel();
1069         string const lang_begin_command = runparams.use_polyglossia ?
1070                 "\\begin{$$lang}" : lyxrc.language_command_begin;
1071
1072         if (maintext && !lyxrc.language_auto_begin &&
1073             !mainlang.empty()) {
1074                 // FIXME UNICODE
1075                 os << from_utf8(subst(lang_begin_command,
1076                                         "$$lang",
1077                                         mainlang));
1078                 if (runparams.use_polyglossia
1079                     && !bparams.language->polyglossiaOpts().empty())
1080                         os << "["
1081                             << from_ascii(bparams.language->polyglossiaOpts())
1082                             << "]";
1083                 os << '\n';
1084         }
1085
1086         ParagraphList const & paragraphs = text.paragraphs();
1087
1088         if (runparams.par_begin == runparams.par_end) {
1089                 // The full doc will be exported but it is easier to just rely on
1090                 // runparams range parameters that will be passed TeXEnvironment.
1091                 runparams.par_begin = 0;
1092                 runparams.par_end = paragraphs.size();
1093         }
1094
1095         pit_type pit = runparams.par_begin;
1096         // lastpit is for the language check after the loop.
1097         pit_type lastpit = pit;
1098         // variables used in the loop:
1099         bool was_title = false;
1100         bool already_title = false;
1101         DocumentClass const & tclass = bparams.documentClass();
1102
1103         for (; pit < runparams.par_end; ++pit) {
1104                 lastpit = pit;
1105                 ParagraphList::const_iterator par = paragraphs.constIterator(pit);
1106
1107                 // FIXME This check should not be needed. We should
1108                 // perhaps issue an error if it is.
1109                 Layout const & layout = text.inset().forcePlainLayout() ?
1110                                 tclass.plainLayout() : par->layout();
1111
1112                 if (layout.intitle) {
1113                         if (already_title) {
1114                                 LYXERR0("Error in latexParagraphs: You"
1115                                         " should not mix title layouts"
1116                                         " with normal ones.");
1117                         } else if (!was_title) {
1118                                 was_title = true;
1119                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1120                                         os << "\\begin{"
1121                                                         << from_ascii(tclass.titlename())
1122                                                         << "}\n";
1123                                 }
1124                         }
1125                 } else if (was_title && !already_title) {
1126                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
1127                                 os << "\\end{" << from_ascii(tclass.titlename())
1128                                                 << "}\n";
1129                         }
1130                         else {
1131                                 os << "\\" << from_ascii(tclass.titlename())
1132                                                 << "\n";
1133                         }
1134                         already_title = true;
1135                         was_title = false;
1136                 }
1137
1138
1139                 if (!layout.isEnvironment() && par->params().leftIndent().zero()) {
1140                         // This is a standard top level paragraph, TeX it and continue.
1141                         TeXOnePar(buf, text, pit, os, runparams, everypar);
1142                         continue;
1143                 }
1144                 
1145                 TeXEnvironmentData const data =
1146                         prepareEnvironment(buf, text, par, os, runparams);
1147                 // pit can be changed in TeXEnvironment.
1148                 TeXEnvironment(buf, text, runparams, pit, os);
1149                 finishEnvironment(os, runparams, data);
1150         }
1151
1152         if (pit == runparams.par_end) {
1153                         // Make sure that the last paragraph is
1154                         // correctly terminated (because TeXOnePar does
1155                         // not add a \n in this case)
1156                         //os << '\n';
1157         }
1158
1159         // It might be that we only have a title in this document
1160         if (was_title && !already_title) {
1161                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
1162                         os << "\\end{" << from_ascii(tclass.titlename())
1163                            << "}\n";
1164                 } else {
1165                         os << "\\" << from_ascii(tclass.titlename())
1166                            << "\n";
1167                 }
1168         }
1169
1170         // if "auto end" is switched off, explicitly close the language at the end
1171         // but only if the last par is in a babel language
1172         string const lang_end_command = runparams.use_polyglossia ?
1173                 "\\end{$$lang}" : lyxrc.language_command_end;
1174         if (maintext && !lyxrc.language_auto_end && !mainlang.empty() &&
1175                 paragraphs.at(lastpit).getParLanguage(bparams)->encoding()->package() != Encoding::CJK) {
1176                 os << from_utf8(subst(lang_end_command,
1177                                         "$$lang",
1178                                         mainlang))
1179                         << '\n';
1180         }
1181
1182         // If the last paragraph is an environment, we'll have to close
1183         // CJK at the very end to do proper nesting.
1184         if (maintext && !is_child && open_encoding_ == CJK) {
1185                 os << "\\end{CJK}\n";
1186                 open_encoding_ = none;
1187         }
1188
1189         // reset inherited encoding
1190         if (cjk_inherited_ > 0) {
1191                 cjk_inherited_ -= 1;
1192                 if (cjk_inherited_ == 0)
1193                         open_encoding_ = CJK;
1194         }
1195 }
1196
1197
1198 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
1199                    OutputParams const & runparams, Encoding const & newEnc,
1200                    bool force)
1201 {
1202         Encoding const & oldEnc = *runparams.encoding;
1203         bool moving_arg = runparams.moving_arg;
1204         // If we switch from/to CJK, we need to switch anyway, despite custom inputenc
1205         bool const from_to_cjk = 
1206                 (oldEnc.package() == Encoding::CJK && newEnc.package() != Encoding::CJK)
1207                 || (oldEnc.package() != Encoding::CJK && newEnc.package() == Encoding::CJK);
1208         if (!force && !from_to_cjk
1209             && ((bparams.inputenc != "auto" && bparams.inputenc != "default") || moving_arg))
1210                 return make_pair(false, 0);
1211
1212         // Do nothing if the encoding is unchanged.
1213         if (oldEnc.name() == newEnc.name())
1214                 return make_pair(false, 0);
1215
1216         // FIXME We ignore encoding switches from/to encodings that do
1217         // neither support the inputenc package nor the CJK package here.
1218         // This does of course only work in special cases (e.g. switch from
1219         // tis620-0 to latin1, but the text in latin1 contains ASCII only),
1220         // but it is the best we can do
1221         if (oldEnc.package() == Encoding::none
1222                 || newEnc.package() == Encoding::none)
1223                 return make_pair(false, 0);
1224
1225         LYXERR(Debug::LATEX, "Changing LaTeX encoding from "
1226                 << oldEnc.name() << " to " << newEnc.name());
1227         os << setEncoding(newEnc.iconvName());
1228         if (bparams.inputenc == "default")
1229                 return make_pair(true, 0);
1230
1231         docstring const inputenc_arg(from_ascii(newEnc.latexName()));
1232         switch (newEnc.package()) {
1233                 case Encoding::none:
1234                 case Encoding::japanese:
1235                         // shouldn't ever reach here, see above
1236                         return make_pair(true, 0);
1237                 case Encoding::inputenc: {
1238                         int count = inputenc_arg.length();
1239                         if (oldEnc.package() == Encoding::CJK &&
1240                             open_encoding_ == CJK) {
1241                                 os << "\\end{CJK}";
1242                                 open_encoding_ = none;
1243                                 count += 9;
1244                         }
1245                         else if (oldEnc.package() == Encoding::inputenc &&
1246                                  open_encoding_ == inputenc) {
1247                                 os << "\\egroup";
1248                                 open_encoding_ = none;
1249                                 count += 7;
1250                         }
1251                         if (runparams.local_font != 0
1252                             &&  oldEnc.package() == Encoding::CJK) {
1253                                 // within insets, \inputenc switches need
1254                                 // to be embraced within \bgroup...\egroup;
1255                                 // else CJK fails.
1256                                 os << "\\bgroup";
1257                                 count += 7;
1258                                 open_encoding_ = inputenc;
1259                         }
1260                         // with the japanese option, inputenc is omitted.
1261                         if (runparams.use_japanese)
1262                                 return make_pair(true, count);
1263                         os << "\\inputencoding{" << inputenc_arg << '}';
1264                         return make_pair(true, count + 16);
1265                 }
1266                 case Encoding::CJK: {
1267                         int count = inputenc_arg.length();
1268                         if (oldEnc.package() == Encoding::CJK &&
1269                             open_encoding_ == CJK) {
1270                                 os << "\\end{CJK}";
1271                                 count += 9;
1272                         }
1273                         if (oldEnc.package() == Encoding::inputenc &&
1274                             open_encoding_ == inputenc) {
1275                                 os << "\\egroup";
1276                                 count += 7;
1277                         }
1278                         os << "\\begin{CJK}{" << inputenc_arg << "}{"
1279                            << from_ascii(bparams.fonts_cjk) << "}";
1280                         open_encoding_ = CJK;
1281                         return make_pair(true, count + 15);
1282                 }
1283         }
1284         // Dead code to avoid a warning:
1285         return make_pair(true, 0);
1286
1287 }
1288
1289 } // namespace lyx