]> git.lyx.org Git - lyx.git/blob - src/output_latex.cpp
Fix "default" encoding to match "auto" encoding except for the actual output
[lyx.git] / src / output_latex.cpp
1 /**
2  * \file output_latex.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "output_latex.h"
14
15 #include "Buffer.h"
16 #include "BufferParams.h"
17 #include "debug.h"
18 #include "Encoding.h"
19 #include "Language.h"
20 #include "LyXRC.h"
21 #include "OutputParams.h"
22 #include "Paragraph.h"
23 #include "paragraph_funcs.h"
24 #include "ParagraphParameters.h"
25 #include "TexRow.h"
26 #include "VSpace.h"
27
28 #include "insets/InsetBibitem.h"
29 #include "insets/InsetOptArg.h"
30
31 #include "support/lstrings.h"
32
33
34 namespace lyx {
35
36 using support::subst;
37
38 using std::endl;
39 using std::string;
40 using std::pair;
41 using std::make_pair;
42
43
44 namespace {
45
46 ParagraphList::const_iterator
47 TeXEnvironment(Buffer const & buf,
48                ParagraphList const & paragraphs,
49                ParagraphList::const_iterator pit,
50                odocstream & os, TexRow & texrow,
51                OutputParams const & runparams);
52
53 ParagraphList::const_iterator
54 TeXOnePar(Buffer const & buf,
55           ParagraphList const & paragraphs,
56           ParagraphList::const_iterator pit,
57           odocstream & os, TexRow & texrow,
58           OutputParams const & runparams,
59           string const & everypar = string());
60
61
62 ParagraphList::const_iterator
63 TeXDeeper(Buffer const & buf,
64           ParagraphList const & paragraphs,
65           ParagraphList::const_iterator pit,
66           odocstream & os, TexRow & texrow,
67           OutputParams const & runparams)
68 {
69         LYXERR(Debug::LATEX) << "TeXDeeper...     " << &*pit << endl;
70         ParagraphList::const_iterator par = pit;
71
72         while (par != paragraphs.end() &&
73                      par->params().depth() == pit->params().depth()) {
74                 if (par->layout()->isEnvironment()) {
75                         par = TeXEnvironment(buf, paragraphs, par,
76                                              os, texrow, runparams);
77                 } else {
78                         par = TeXOnePar(buf, paragraphs, par,
79                                              os, texrow, runparams);
80                 }
81         }
82         LYXERR(Debug::LATEX) << "TeXDeeper...done " << endl;
83
84         return par;
85 }
86
87
88 ParagraphList::const_iterator
89 TeXEnvironment(Buffer const & buf,
90                ParagraphList const & paragraphs,
91                ParagraphList::const_iterator pit,
92                odocstream & os, TexRow & texrow,
93                OutputParams const & runparams)
94 {
95         LYXERR(Debug::LATEX) << "TeXEnvironment...     " << &*pit << endl;
96
97         BufferParams const & bparams = buf.params();
98
99         Layout_ptr const & style = pit->layout();
100
101         Language const * const par_language = pit->getParLanguage(bparams);
102         Language const * const doc_language = bparams.language;
103         Language const * const prev_par_language =
104                 (pit != paragraphs.begin())
105                 ? boost::prior(pit)->getParLanguage(bparams)
106                 : doc_language;
107         if (par_language->babel() != prev_par_language->babel()) {
108
109                 if (!lyxrc.language_command_end.empty() &&
110                     prev_par_language->babel() != doc_language->babel() &&
111                     !prev_par_language->babel().empty()) {
112                         os << from_ascii(subst(
113                                 lyxrc.language_command_end,
114                                 "$$lang",
115                                 prev_par_language->babel()))
116                            << '\n';
117                         texrow.newline();
118                 }
119
120                 if ((lyxrc.language_command_end.empty() ||
121                      par_language->babel() != doc_language->babel()) &&
122                     !par_language->babel().empty()) {
123                         os << from_ascii(subst(
124                                 lyxrc.language_command_begin,
125                                 "$$lang",
126                                 par_language->babel()))
127                            << '\n';
128                         texrow.newline();
129                 }
130         }
131
132         bool leftindent_open = false;
133         if (!pit->params().leftIndent().zero()) {
134                 os << "\\begin{LyXParagraphLeftIndent}{"
135                    << from_ascii(pit->params().leftIndent().asLatexString())
136                    << "}\n";
137                 texrow.newline();
138                 leftindent_open = true;
139         }
140
141         if (style->isEnvironment()) {
142                 os << "\\begin{" << from_ascii(style->latexname()) << '}';
143                 if (style->optionalargs > 0) {
144                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
145                                                     style->optionalargs);
146                         while (ret > 0) {
147                                 texrow.newline();
148                                 --ret;
149                         }
150                 }
151                 if (style->latextype == LATEX_LIST_ENVIRONMENT) {
152                         os << '{'
153                            << pit->params().labelWidthString()
154                            << "}\n";
155                 } else if (style->labeltype == LABEL_BIBLIO) {
156                         // ale970405
157                         os << '{' << bibitemWidest(buf) << "}\n";
158                 } else
159                         os << from_ascii(style->latexparam()) << '\n';
160                 texrow.newline();
161         }
162         ParagraphList::const_iterator par = pit;
163         do {
164                 par = TeXOnePar(buf, paragraphs, par, os, texrow, runparams);
165
166                 if (par == paragraphs.end()) {
167                         // Make sure that the last paragraph is
168                         // correctly terminated (because TeXOnePar does
169                         // not add a \n in this case)
170                         os << '\n';
171                         texrow.newline();
172                 } else if (par->params().depth() > pit->params().depth()) {
173                             if (par->layout()->isParagraph()) {
174
175                             // Thinko!
176                             // How to handle this? (Lgb)
177                             //&& !suffixIs(os, "\n\n")
178                                     //) {
179                                 // There should be at least one '\n' already
180                                 // but we need there to be two for Standard
181                                 // paragraphs that are depth-increment'ed to be
182                                 // output correctly.  However, tables can
183                                 // also be paragraphs so don't adjust them.
184                                 // ARRae
185                                 // Thinkee:
186                                 // Will it ever harm to have one '\n' too
187                                 // many? i.e. that we sometimes will have
188                                 // three in a row. (Lgb)
189                                 os << '\n';
190                                 texrow.newline();
191                         }
192                         par = TeXDeeper(buf, paragraphs, par, os, texrow,
193                                         runparams);
194                 }
195         } while (par != paragraphs.end()
196                  && par->layout() == pit->layout()
197                  && par->params().depth() == pit->params().depth()
198                  && par->params().leftIndent() == pit->params().leftIndent());
199
200         if (style->isEnvironment()) {
201                 os << "\\end{" << from_ascii(style->latexname()) << "}\n";
202                 texrow.newline();
203         }
204
205         if (leftindent_open) {
206                 os << "\\end{LyXParagraphLeftIndent}\n";
207                 texrow.newline();
208         }
209
210         if (par != paragraphs.end())
211                 LYXERR(Debug::LATEX) << "TeXEnvironment...done " << &*par << endl;
212         return par;
213 }
214
215 }
216
217
218 int latexOptArgInsets(Buffer const & buf, Paragraph const & par,
219                       odocstream & os, OutputParams const & runparams, int number)
220 {
221         int lines = 0;
222
223         InsetList::const_iterator it = par.insetlist.begin();
224         InsetList::const_iterator end = par.insetlist.end();
225         for (; it != end && number > 0 ; ++it) {
226                 if (it->inset->lyxCode() == Inset::OPTARG_CODE) {
227                         InsetOptArg * ins =
228                                 static_cast<InsetOptArg *>(it->inset);
229                         lines += ins->latexOptional(buf, os, runparams);
230                         --number;
231                 }
232         }
233         return lines;
234 }
235
236
237 namespace {
238
239 ParagraphList::const_iterator
240 TeXOnePar(Buffer const & buf,
241           ParagraphList const & paragraphs,
242           ParagraphList::const_iterator pit,
243           odocstream & os, TexRow & texrow,
244           OutputParams const & runparams_in,
245           string const & everypar)
246 {
247         LYXERR(Debug::LATEX) << "TeXOnePar...     " << &*pit << " '"
248                 << everypar << "'" << endl;
249         BufferParams const & bparams = buf.params();
250         Layout_ptr style;
251
252         // In an inset with unlimited length (all in one row),
253         // force layout to default
254         if (!pit->forceDefaultParagraphs())
255                 style = pit->layout();
256         else
257                 style = bparams.getTextClass().defaultLayout();
258
259         OutputParams runparams = runparams_in;
260         runparams.moving_arg |= style->needprotect;
261
262         Language const * const par_language = pit->getParLanguage(bparams);
263         Language const * const doc_language = bparams.language;
264         Language const * const prev_par_language =
265                 (pit != paragraphs.begin())
266                 ? boost::prior(pit)->getParLanguage(bparams)
267                 : doc_language;
268
269         if (par_language->babel() != prev_par_language->babel()
270             // check if we already put language command in TeXEnvironment()
271             && !(style->isEnvironment()
272                  && (pit == paragraphs.begin() ||
273                      (boost::prior(pit)->layout() != pit->layout() &&
274                       boost::prior(pit)->getDepth() <= pit->getDepth())
275                      || boost::prior(pit)->getDepth() < pit->getDepth())))
276         {
277                 if (!lyxrc.language_command_end.empty() &&
278                     prev_par_language->babel() != doc_language->babel() &&
279                     !prev_par_language->babel().empty())
280                 {
281                         os << from_ascii(subst(lyxrc.language_command_end,
282                                 "$$lang",
283                                 prev_par_language->babel()))
284                            << '\n';
285                         texrow.newline();
286                 }
287
288                 if ((lyxrc.language_command_end.empty() ||
289                      par_language->babel() != doc_language->babel()) &&
290                     !par_language->babel().empty()) {
291                         os << from_ascii(subst(
292                                 lyxrc.language_command_begin,
293                                 "$$lang",
294                                 par_language->babel()))
295                            << '\n';
296                         texrow.newline();
297                 }
298         }
299
300         // Switch file encoding if necessary; no need to do this for "default"
301         // encoding, since this only affects the position of the outputted
302         // \inputencoding command; the encoding switch will occur when necessary
303         if (bparams.inputenc == "auto" &&
304             runparams.encoding->package() == Encoding::inputenc) {
305                 // Look ahead for future encoding changes.
306                 // We try to output them at the beginning of the paragraph,
307                 // since the \inputencoding command is not allowed e.g. in
308                 // sections.
309                 for (pos_type i = 0; i < pit->size(); ++i) {
310                         char_type const c = pit->getChar(i);
311                         if (c < 0x80)
312                                 continue;
313                         if (pit->isInset(i))
314                                 break;
315                         // All characters before c are in the ASCII range, and
316                         // c is non-ASCII (but no inset), so change the
317                         // encoding to that required by the language of c.
318                         Encoding const * const encoding =
319                                 pit->getFontSettings(bparams, i).language()->encoding();
320                         pair<bool, int> enc_switch = switchEncoding(os, bparams, false,
321                                         *(runparams.encoding), *encoding);
322                         if (encoding->package() == Encoding::inputenc && enc_switch.first) {
323                                 runparams.encoding = encoding;
324                                 if (enc_switch.second > 0) {
325                                         os << '\n';
326                                         texrow.newline();
327                                 }
328                         }
329                         break;
330                 }
331         }
332
333         // In an inset with unlimited length (all in one row),
334         // don't allow any special options in the paragraph
335         if (!pit->forceDefaultParagraphs()) {
336                 if (pit->params().startOfAppendix()) {
337                         os << "\\appendix\n";
338                         texrow.newline();
339                 }
340
341                 if (!pit->params().spacing().isDefault()
342                         && (pit == paragraphs.begin()
343                             || !boost::prior(pit)->hasSameLayout(*pit)))
344                 {
345                         os << from_ascii(pit->params().spacing().writeEnvirBegin())
346                             << '\n';
347                         texrow.newline();
348                 }
349
350                 if (style->isCommand()) {
351                         os << '\n';
352                         texrow.newline();
353                 }
354         }
355
356         switch (style->latextype) {
357         case LATEX_COMMAND:
358                 os << '\\' << from_ascii(style->latexname());
359
360                 // Separate handling of optional argument inset.
361                 if (style->optionalargs > 0) {
362                         int ret = latexOptArgInsets(buf, *pit, os, runparams,
363                                                     style->optionalargs);
364                         while (ret > 0) {
365                                 texrow.newline();
366                                 --ret;
367                         }
368                 }
369                 else
370                         os << from_ascii(style->latexparam());
371                 break;
372         case LATEX_ITEM_ENVIRONMENT:
373         case LATEX_LIST_ENVIRONMENT:
374                 os << "\\item ";
375                 break;
376         case LATEX_BIB_ENVIRONMENT:
377                 // ignore this, the inset will write itself
378                 break;
379         default:
380                 break;
381         }
382
383         Font const outerfont =
384                 outerFont(std::distance(paragraphs.begin(), pit),
385                           paragraphs);
386
387         // FIXME UNICODE
388         os << from_utf8(everypar);
389         bool need_par = pit->simpleTeXOnePar(buf, bparams, outerfont,
390                                              os, texrow, runparams);
391
392         // Make sure that \\par is done with the font of the last
393         // character if this has another size as the default.
394         // This is necessary because LaTeX (and LyX on the screen)
395         // calculates the space between the baselines according
396         // to this font. (Matthias)
397         //
398         // Is this really needed ? (Dekel)
399         // We do not need to use to change the font for the last paragraph
400         // or for a command.
401
402         Font const font =
403                 (pit->empty()
404                  ? pit->getLayoutFont(bparams, outerfont)
405                  : pit->getFont(bparams, pit->size() - 1, outerfont));
406
407         bool is_command = style->isCommand();
408
409         if (style->resfont.size() != font.size()
410             && boost::next(pit) != paragraphs.end()
411             && !is_command) {
412                 if (!need_par)
413                         os << '{';
414                 os << "\\" << from_ascii(font.latexSize()) << " \\par}";
415         } else if (need_par) {
416                 os << "\\par}";
417         } else if (is_command)
418                 os << '}';
419
420         bool pending_newline = false;
421         switch (style->latextype) {
422         case LATEX_ITEM_ENVIRONMENT:
423         case LATEX_LIST_ENVIRONMENT:
424                 if (boost::next(pit) != paragraphs.end()
425                     && (pit->params().depth() < boost::next(pit)->params().depth()))
426                         pending_newline = true;
427                 break;
428         case LATEX_ENVIRONMENT: {
429                 // if its the last paragraph of the current environment
430                 // skip it otherwise fall through
431                 ParagraphList::const_iterator next = boost::next(pit);
432
433                 if (next != paragraphs.end()
434                     && (next->layout() != pit->layout()
435                         || next->params().depth() != pit->params().depth()))
436                         break;
437         }
438
439                 // fall through possible
440         default:
441                 // we don't need it for the last paragraph!!!
442                 if (boost::next(pit) != paragraphs.end())
443                         pending_newline = true;
444         }
445
446         if (!pit->forceDefaultParagraphs()) {
447                 if (!pit->params().spacing().isDefault()
448                         && (boost::next(pit) == paragraphs.end()
449                             || !boost::next(pit)->hasSameLayout(*pit)))
450                 {
451                         if (pending_newline) {
452                                 os << '\n';
453                                 texrow.newline();
454                         }
455                         os << from_ascii(pit->params().spacing().writeEnvirEnd());
456                         pending_newline = true;
457                 }
458         }
459
460         if (boost::next(pit) == paragraphs.end()
461             && par_language->babel() != doc_language->babel()) {
462                 // Since \selectlanguage write the language to the aux file,
463                 // we need to reset the language at the end of footnote or
464                 // float.
465
466                 if (pending_newline) {
467                         os << '\n';
468                         texrow.newline();
469                 }
470                 if (lyxrc.language_command_end.empty()) {
471                         if (!doc_language->babel().empty()) {
472                                 os << from_ascii(subst(
473                                         lyxrc.language_command_begin,
474                                         "$$lang",
475                                         doc_language->babel()));
476                                 pending_newline = true;
477                         }
478                 } else if (!par_language->babel().empty()) {
479                         os << from_ascii(subst(
480                                 lyxrc.language_command_end,
481                                 "$$lang",
482                                 par_language->babel()));
483                         pending_newline = true;
484                 }
485         }
486
487         if (pending_newline) {
488                 os << '\n';
489                 texrow.newline();
490         }
491         runparams_in.encoding = runparams.encoding;
492
493         // we don't need it for the last paragraph!!!
494         // Note from JMarc: we will re-add a \n explicitely in
495         // TeXEnvironment, because it is needed in this case
496         if (boost::next(pit) != paragraphs.end()) {
497                 os << '\n';
498                 texrow.newline();
499         }
500
501         if (boost::next(pit) != paragraphs.end())
502                 LYXERR(Debug::LATEX) << "TeXOnePar...done " << &*boost::next(pit) << endl;
503
504         return ++pit;
505 }
506
507 } // anon namespace
508
509
510 // LaTeX all paragraphs
511 void latexParagraphs(Buffer const & buf,
512                      ParagraphList const & paragraphs,
513                      odocstream & os,
514                      TexRow & texrow,
515                      OutputParams const & runparams,
516                      string const & everypar)
517 {
518         bool was_title = false;
519         bool already_title = false;
520         TextClass const & tclass = buf.params().getTextClass();
521         ParagraphList::const_iterator par = paragraphs.begin();
522         ParagraphList::const_iterator endpar = paragraphs.end();
523
524         BOOST_ASSERT(runparams.par_begin <= runparams.par_end);
525         // if only part of the paragraphs will be outputed
526         if (runparams.par_begin !=  runparams.par_end) {
527                 par = boost::next(paragraphs.begin(), runparams.par_begin);
528                 endpar = boost::next(paragraphs.begin(), runparams.par_end);
529                 // runparams will be passed to nested paragraphs, so
530                 // we have to reset the range parameters.
531                 const_cast<OutputParams&>(runparams).par_begin = 0;
532                 const_cast<OutputParams&>(runparams).par_end = 0;
533         }
534
535         // if only_body
536         while (par != endpar) {
537                 ParagraphList::const_iterator lastpar = par;
538                 // well we have to check if we are in an inset with unlimited
539                 // length (all in one row) if that is true then we don't allow
540                 // any special options in the paragraph and also we don't allow
541                 // any environment other than the default layout of the
542                 // text class to be valid!
543                 if (!par->forceDefaultParagraphs()) {
544                         Layout_ptr const & layout = par->layout();
545
546                         if (layout->intitle) {
547                                 if (already_title) {
548                                         lyxerr << "Error in latexParagraphs: You"
549                                                 " should not mix title layouts"
550                                                 " with normal ones." << endl;
551                                 } else if (!was_title) {
552                                         was_title = true;
553                                         if (tclass.titletype() == TITLE_ENVIRONMENT) {
554                                                 os << "\\begin{"
555                                                     << from_ascii(tclass.titlename())
556                                                     << "}\n";
557                                                 texrow.newline();
558                                         }
559                                 }
560                         } else if (was_title && !already_title) {
561                                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
562                                         os << "\\end{" << from_ascii(tclass.titlename())
563                                             << "}\n";
564                                 }
565                                 else {
566                                         os << "\\" << from_ascii(tclass.titlename())
567                                             << "\n";
568                                 }
569                                 texrow.newline();
570                                 already_title = true;
571                                 was_title = false;
572                         }
573
574                         if (layout->is_environment) {
575                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
576                                                 runparams, everypar);
577                         } else if (layout->isEnvironment() ||
578                                    !par->params().leftIndent().zero()) {
579                                 par = TeXEnvironment(buf, paragraphs, par, os,
580                                                      texrow, runparams);
581                         } else {
582                                 par = TeXOnePar(buf, paragraphs, par, os, texrow,
583                                                 runparams, everypar);
584                         }
585                 } else {
586                         par = TeXOnePar(buf, paragraphs, par, os, texrow,
587                                         runparams, everypar);
588                 }
589                 if (std::distance(lastpar, par) >= std::distance(lastpar, endpar))
590                         break;
591         }
592         // It might be that we only have a title in this document
593         if (was_title && !already_title) {
594                 if (tclass.titletype() == TITLE_ENVIRONMENT) {
595                         os << "\\end{" << from_ascii(tclass.titlename())
596                             << "}\n";
597                 }
598                 else {
599                         os << "\\" << from_ascii(tclass.titlename())
600                             << "\n";
601                                 }
602                 texrow.newline();
603         }
604 }
605
606
607 pair<bool, int> switchEncoding(odocstream & os, BufferParams const & bparams,
608                    bool moving_arg, Encoding const & oldEnc,
609                    Encoding const & newEnc)
610 {
611         if ((bparams.inputenc != "auto" && bparams.inputenc != "default")
612                 || moving_arg)
613                 return make_pair(false, 0);
614
615         // Do nothing if the encoding is unchanged.
616         if (oldEnc.name() == newEnc.name())
617                 return make_pair(false, 0);
618
619         // FIXME We ignore encoding switches from/to encodings that do
620         // neither support the inputenc package nor the CJK package here.
621         // This does of course only work in special cases (e.g. switch from
622         // tis620-0 to latin1, but the text in latin1 contains ASCII only),
623         // but it is the best we can do
624         if (oldEnc.package() == Encoding::none
625                 || newEnc.package() == Encoding::none)
626                 return make_pair(false, 0);
627
628         LYXERR(Debug::LATEX) << "Changing LaTeX encoding from "
629                 << oldEnc.name() << " to "
630                 << newEnc.name() << endl;
631         os << setEncoding(newEnc.iconvName());
632         if (bparams.inputenc == "default")
633                 return make_pair(true, 0);
634
635         docstring const inputenc(from_ascii(newEnc.latexName()));
636         switch (newEnc.package()) {
637                 case Encoding::none:
638                         // shouldn't ever reach here, see above
639                         return make_pair(true, 0);
640                 case Encoding::inputenc: {
641                         int count = inputenc.length();
642                         if (oldEnc.package() == Encoding::CJK) {
643                                 os << "\\end{CJK}";
644                                 count += 9;
645                         }
646                         os << "\\inputencoding{" << inputenc << '}';
647                         return make_pair(true, count + 16);
648                  }
649                 case Encoding::CJK: {
650                         int count = inputenc.length();
651                         if (oldEnc.package() == Encoding::CJK) {
652                                 os << "\\end{CJK}";
653                                 count += 9;
654                         }
655                         os << "\\begin{CJK}{" << inputenc << "}{}";
656                         return make_pair(true, count + 15);
657                 }
658         }
659         // Dead code to avoid a warning:
660         return make_pair(true, 0);
661 }
662
663 } // namespace lyx