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