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