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