]> git.lyx.org Git - lyx.git/blob - src/BufferParams.cpp
Revert "Fix bug with wrong baseline calculation in last paragraph"
[lyx.git] / src / BufferParams.cpp
1 /**
2  * \file BufferParams.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author Jean-Marc Lasgouttes
9  * \author John Levon
10  * \author André Pönitz
11  * \author Martin Vermeer
12  *
13  * Full author contact details are available in file CREDITS.
14  */
15
16 #include <config.h>
17
18 #include "BufferParams.h"
19
20 #include "Author.h"
21 #include "LayoutFile.h"
22 #include "BranchList.h"
23 #include "buffer_funcs.h"
24 #include "Bullet.h"
25 #include "Color.h"
26 #include "ColorSet.h"
27 #include "Converter.h"
28 #include "Encoding.h"
29 #include "HSpace.h"
30 #include "IndicesList.h"
31 #include "Language.h"
32 #include "LaTeXFeatures.h"
33 #include "LaTeXFonts.h"
34 #include "ModuleList.h"
35 #include "Font.h"
36 #include "Lexer.h"
37 #include "LyXRC.h"
38 #include "OutputParams.h"
39 #include "Spacing.h"
40 #include "TexRow.h"
41 #include "VSpace.h"
42 #include "PDFOptions.h"
43
44 #include "frontends/alert.h"
45
46 #include "insets/InsetListingsParams.h"
47
48 #include "support/convert.h"
49 #include "support/debug.h"
50 #include "support/docstream.h"
51 #include "support/FileName.h"
52 #include "support/filetools.h"
53 #include "support/gettext.h"
54 #include "support/Messages.h"
55 #include "support/mutex.h"
56 #include "support/Translator.h"
57 #include "support/lstrings.h"
58
59 #include <algorithm>
60 #include <sstream>
61
62 using namespace std;
63 using namespace lyx::support;
64
65
66 static char const * const string_paragraph_separation[] = {
67         "indent", "skip", ""
68 };
69
70
71 static char const * const string_quotes_language[] = {
72         "english", "swedish", "german", "polish", "french", "danish", ""
73 };
74
75
76 static char const * const string_papersize[] = {
77         "default", "custom", "letterpaper", "legalpaper", "executivepaper",
78         "a0paper", "a1paper", "a2paper", "a3paper",     "a4paper", "a5paper",
79         "a6paper", "b0paper", "b1paper", "b2paper","b3paper", "b4paper",
80         "b5paper", "b6paper", "c0paper", "c1paper", "c2paper", "c3paper",
81         "c4paper", "c5paper", "c6paper", "b0j", "b1j", "b2j", "b3j", "b4j", "b5j",
82         "b6j", ""
83 };
84
85
86 static char const * const string_orientation[] = {
87         "portrait", "landscape", ""
88 };
89
90
91 static char const * const tex_graphics[] = {
92         "default", "dvialw", "dvilaser", "dvipdf", "dvipdfm", "dvipdfmx",
93         "dvips", "dvipsone", "dvitops", "dviwin", "dviwindo", "dvi2ps", "emtex",
94         "ln", "oztex", "pctexhp", "pctexps", "pctexwin", "pctex32", "pdftex",
95         "psprint", "pubps", "tcidvi", "textures", "truetex", "vtex", "xdvi",
96         "xetex", "none", ""
97 };
98
99
100
101 namespace lyx {
102
103 // Local translators
104 namespace {
105
106 // Paragraph separation
107 typedef Translator<string, BufferParams::ParagraphSeparation> ParSepTranslator;
108
109
110 ParSepTranslator const init_parseptranslator()
111 {
112         ParSepTranslator translator
113                 (string_paragraph_separation[0], BufferParams::ParagraphIndentSeparation);
114         translator.addPair(string_paragraph_separation[1], BufferParams::ParagraphSkipSeparation);
115         return translator;
116 }
117
118
119 ParSepTranslator const & parseptranslator()
120 {
121         static ParSepTranslator const translator =
122                 init_parseptranslator();
123         return translator;
124 }
125
126
127 // Quotes language
128 typedef Translator<string, InsetQuotes::QuoteLanguage> QuotesLangTranslator;
129
130
131 QuotesLangTranslator const init_quoteslangtranslator()
132 {
133         QuotesLangTranslator translator
134                 (string_quotes_language[0], InsetQuotes::EnglishQuotes);
135         translator.addPair(string_quotes_language[1], InsetQuotes::SwedishQuotes);
136         translator.addPair(string_quotes_language[2], InsetQuotes::GermanQuotes);
137         translator.addPair(string_quotes_language[3], InsetQuotes::PolishQuotes);
138         translator.addPair(string_quotes_language[4], InsetQuotes::FrenchQuotes);
139         translator.addPair(string_quotes_language[5], InsetQuotes::DanishQuotes);
140         return translator;
141 }
142
143
144 QuotesLangTranslator const & quoteslangtranslator()
145 {
146         static QuotesLangTranslator const translator =
147                 init_quoteslangtranslator();
148         return translator;
149 }
150
151
152 // Paper size
153 typedef Translator<string, PAPER_SIZE> PaperSizeTranslator;
154
155
156 static PaperSizeTranslator initPaperSizeTranslator()
157 {
158         PaperSizeTranslator translator(string_papersize[0], PAPER_DEFAULT);
159         translator.addPair(string_papersize[1], PAPER_CUSTOM);
160         translator.addPair(string_papersize[2], PAPER_USLETTER);
161         translator.addPair(string_papersize[3], PAPER_USLEGAL);
162         translator.addPair(string_papersize[4], PAPER_USEXECUTIVE);
163         translator.addPair(string_papersize[5], PAPER_A0);
164         translator.addPair(string_papersize[6], PAPER_A1);
165         translator.addPair(string_papersize[7], PAPER_A2);
166         translator.addPair(string_papersize[8], PAPER_A3);
167         translator.addPair(string_papersize[9], PAPER_A4);
168         translator.addPair(string_papersize[10], PAPER_A5);
169         translator.addPair(string_papersize[11], PAPER_A6);
170         translator.addPair(string_papersize[12], PAPER_B0);
171         translator.addPair(string_papersize[13], PAPER_B1);
172         translator.addPair(string_papersize[14], PAPER_B2);
173         translator.addPair(string_papersize[15], PAPER_B3);
174         translator.addPair(string_papersize[16], PAPER_B4);
175         translator.addPair(string_papersize[17], PAPER_B5);
176         translator.addPair(string_papersize[18], PAPER_B6);
177         translator.addPair(string_papersize[19], PAPER_C0);
178         translator.addPair(string_papersize[20], PAPER_C1);
179         translator.addPair(string_papersize[21], PAPER_C2);
180         translator.addPair(string_papersize[22], PAPER_C3);
181         translator.addPair(string_papersize[23], PAPER_C4);
182         translator.addPair(string_papersize[24], PAPER_C5);
183         translator.addPair(string_papersize[25], PAPER_C6);
184         translator.addPair(string_papersize[26], PAPER_JISB0);
185         translator.addPair(string_papersize[27], PAPER_JISB1);
186         translator.addPair(string_papersize[28], PAPER_JISB2);
187         translator.addPair(string_papersize[29], PAPER_JISB3);
188         translator.addPair(string_papersize[30], PAPER_JISB4);
189         translator.addPair(string_papersize[31], PAPER_JISB5);
190         translator.addPair(string_papersize[32], PAPER_JISB6);
191         return translator;
192 }
193
194
195 PaperSizeTranslator const & papersizetranslator()
196 {
197         static PaperSizeTranslator const translator =
198                 initPaperSizeTranslator();
199         return translator;
200 }
201
202
203 // Paper orientation
204 typedef Translator<string, PAPER_ORIENTATION> PaperOrientationTranslator;
205
206
207 PaperOrientationTranslator const init_paperorientationtranslator()
208 {
209         PaperOrientationTranslator translator(string_orientation[0], ORIENTATION_PORTRAIT);
210         translator.addPair(string_orientation[1], ORIENTATION_LANDSCAPE);
211         return translator;
212 }
213
214
215 PaperOrientationTranslator const & paperorientationtranslator()
216 {
217         static PaperOrientationTranslator const translator =
218             init_paperorientationtranslator();
219         return translator;
220 }
221
222
223 // Page sides
224 typedef Translator<int, PageSides> SidesTranslator;
225
226
227 SidesTranslator const init_sidestranslator()
228 {
229         SidesTranslator translator(1, OneSide);
230         translator.addPair(2, TwoSides);
231         return translator;
232 }
233
234
235 SidesTranslator const & sidestranslator()
236 {
237         static SidesTranslator const translator = init_sidestranslator();
238         return translator;
239 }
240
241
242 // LaTeX packages
243 typedef Translator<int, BufferParams::Package> PackageTranslator;
244
245
246 PackageTranslator const init_packagetranslator()
247 {
248         PackageTranslator translator(0, BufferParams::package_off);
249         translator.addPair(1, BufferParams::package_auto);
250         translator.addPair(2, BufferParams::package_on);
251         return translator;
252 }
253
254
255 PackageTranslator const & packagetranslator()
256 {
257         static PackageTranslator const translator =
258                 init_packagetranslator();
259         return translator;
260 }
261
262
263 // Cite engine
264 typedef Translator<string, CiteEngineType> CiteEngineTypeTranslator;
265
266
267 CiteEngineTypeTranslator const init_citeenginetypetranslator()
268 {
269         CiteEngineTypeTranslator translator("authoryear", ENGINE_TYPE_AUTHORYEAR);
270         translator.addPair("numerical", ENGINE_TYPE_NUMERICAL);
271         translator.addPair("default", ENGINE_TYPE_DEFAULT);
272         return translator;
273 }
274
275
276 CiteEngineTypeTranslator const & citeenginetypetranslator()
277 {
278         static CiteEngineTypeTranslator const translator =
279                 init_citeenginetypetranslator();
280         return translator;
281 }
282
283
284 // Spacing
285 typedef Translator<string, Spacing::Space> SpaceTranslator;
286
287
288 SpaceTranslator const init_spacetranslator()
289 {
290         SpaceTranslator translator("default", Spacing::Default);
291         translator.addPair("single", Spacing::Single);
292         translator.addPair("onehalf", Spacing::Onehalf);
293         translator.addPair("double", Spacing::Double);
294         translator.addPair("other", Spacing::Other);
295         return translator;
296 }
297
298
299 SpaceTranslator const & spacetranslator()
300 {
301         static SpaceTranslator const translator = init_spacetranslator();
302         return translator;
303 }
304
305 } // anon namespace
306
307
308 class BufferParams::Impl
309 {
310 public:
311         Impl();
312
313         AuthorList authorlist;
314         BranchList branchlist;
315         Bullet temp_bullets[4];
316         Bullet user_defined_bullets[4];
317         IndicesList indiceslist;
318         Spacing spacing;
319         /** This is the amount of space used for paragraph_separation "skip",
320          * and for detached paragraphs in "indented" documents.
321          */
322         HSpace indentation;
323         VSpace defskip;
324         PDFOptions pdfoptions;
325         LayoutFileIndex baseClass_;
326 };
327
328
329 BufferParams::Impl::Impl()
330         : defskip(VSpace::MEDSKIP), baseClass_(string(""))
331 {
332         // set initial author
333         // FIXME UNICODE
334         authorlist.record(Author(from_utf8(lyxrc.user_name), from_utf8(lyxrc.user_email)));
335 }
336
337
338 BufferParams::Impl *
339 BufferParams::MemoryTraits::clone(BufferParams::Impl const * ptr)
340 {
341         LBUFERR(ptr);
342         return new BufferParams::Impl(*ptr);
343 }
344
345
346 void BufferParams::MemoryTraits::destroy(BufferParams::Impl * ptr)
347 {
348         delete ptr;
349 }
350
351
352 BufferParams::BufferParams()
353         : pimpl_(new Impl)
354 {
355         setBaseClass(defaultBaseclass());
356         makeDocumentClass();
357         paragraph_separation = ParagraphIndentSeparation;
358         quotes_language = InsetQuotes::EnglishQuotes;
359         fontsize = "default";
360
361         /*  PaperLayout */
362         papersize = PAPER_DEFAULT;
363         orientation = ORIENTATION_PORTRAIT;
364         use_geometry = false;
365         cite_engine_.push_back("basic");
366         cite_engine_type_ = ENGINE_TYPE_DEFAULT;
367         biblio_style = "plain";
368         use_bibtopic = false;
369         use_indices = false;
370         track_changes = false;
371         output_changes = false;
372         use_default_options = true;
373         maintain_unincluded_children = false;
374         secnumdepth = 3;
375         tocdepth = 3;
376         language = default_language;
377         fontenc = "global";
378         fonts_roman = "default";
379         fonts_sans = "default";
380         fonts_typewriter = "default";
381         fonts_math = "auto";
382         fonts_default_family = "default";
383         useNonTeXFonts = false;
384         fonts_expert_sc = false;
385         fonts_old_figures = false;
386         fonts_sans_scale = 100;
387         fonts_typewriter_scale = 100;
388         inputenc = "auto";
389         lang_package = "default";
390         graphics_driver = "default";
391         default_output_format = "default";
392         bibtex_command = "default";
393         index_command = "default";
394         sides = OneSide;
395         columns = 1;
396         listings_params = string();
397         pagestyle = "default";
398         suppress_date = false;
399         justification = true;
400         // no color is the default (white)
401         backgroundcolor = lyx::rgbFromHexName("#ffffff");
402         isbackgroundcolor = false;
403         // no color is the default (black)
404         fontcolor = lyx::rgbFromHexName("#000000");
405         isfontcolor = false;
406         // light gray is the default font color for greyed-out notes
407         notefontcolor = lyx::rgbFromHexName("#cccccc");
408         boxbgcolor = lyx::rgbFromHexName("#ff0000");
409         compressed = lyxrc.save_compressed;
410         for (int iter = 0; iter < 4; ++iter) {
411                 user_defined_bullet(iter) = ITEMIZE_DEFAULTS[iter];
412                 temp_bullet(iter) = ITEMIZE_DEFAULTS[iter];
413         }
414         // default index
415         indiceslist().addDefault(B_("Index"));
416         html_be_strict = false;
417         html_math_output = MathML;
418         html_math_img_scale = 1.0;
419         html_css_as_file = false;
420
421         output_sync = false;
422         use_refstyle = true;
423 }
424
425
426 docstring BufferParams::B_(string const & l10n) const
427 {
428         LASSERT(language, return from_utf8(l10n));
429         return getMessages(language->code()).get(l10n);
430 }
431
432
433 BufferParams::Package BufferParams::use_package(std::string const & p) const
434 {
435         PackageMap::const_iterator it = use_packages.find(p);
436         if (it == use_packages.end())
437                 return package_auto;
438         return it->second;
439 }
440
441
442 void BufferParams::use_package(std::string const & p, BufferParams::Package u)
443 {
444         use_packages[p] = u;
445 }
446
447
448 map<string, string> const & BufferParams::auto_packages()
449 {
450         static map<string, string> packages;
451         if (packages.empty()) {
452                 // We could have a race condition here that two threads
453                 // discover an empty map at the same time and want to fill
454                 // it, but that is no problem, since the same contents is
455                 // filled in twice then. Having the locker inside the
456                 // packages.empty() condition has the advantage that we
457                 // don't need the mutex overhead for simple reading.
458                 static Mutex mutex;
459                 Mutex::Locker locker(&mutex);
460                 // adding a package here implies a file format change!
461                 packages["amsmath"] =
462                         N_("The LaTeX package amsmath is only used if AMS formula types or symbols from the AMS math toolbars are inserted into formulas");
463                 packages["amssymb"] =
464                         N_("The LaTeX package amssymb is only used if symbols from the AMS math toolbars are inserted into formulas");
465                 packages["cancel"] =
466                         N_("The LaTeX package cancel is only used if \\cancel commands are used in formulas");
467                 packages["esint"] =
468                         N_("The LaTeX package esint is only used if special integral symbols are inserted into formulas");
469                 packages["mathdots"] =
470                         N_("The LaTeX package mathdots is only used if the command \\iddots is inserted into formulas");
471                 packages["mathtools"] =
472                         N_("The LaTeX package mathtools is only used if some mathematical relations are inserted into formulas");
473                 packages["mhchem"] =
474                         N_("The LaTeX package mhchem is only used if either the command \\ce or \\cf is inserted into formulas");
475                 packages["stackrel"] =
476                         N_("The LaTeX package stackrel is only used if the command \\stackrel with subscript is inserted into formulas");
477                 packages["stmaryrd"] =
478                         N_("The LaTeX package stmaryrd is only used if symbols from the St Mary's Road symbol font for theoretical computer science are inserted into formulas");
479                 packages["undertilde"] =
480                         N_("The LaTeX package undertilde is only used if you use the math frame decoration 'utilde'");
481         }
482         return packages;
483 }
484
485
486 AuthorList & BufferParams::authors()
487 {
488         return pimpl_->authorlist;
489 }
490
491
492 AuthorList const & BufferParams::authors() const
493 {
494         return pimpl_->authorlist;
495 }
496
497
498 BranchList & BufferParams::branchlist()
499 {
500         return pimpl_->branchlist;
501 }
502
503
504 BranchList const & BufferParams::branchlist() const
505 {
506         return pimpl_->branchlist;
507 }
508
509
510 IndicesList & BufferParams::indiceslist()
511 {
512         return pimpl_->indiceslist;
513 }
514
515
516 IndicesList const & BufferParams::indiceslist() const
517 {
518         return pimpl_->indiceslist;
519 }
520
521
522 Bullet & BufferParams::temp_bullet(lyx::size_type const index)
523 {
524         LASSERT(index < 4, return pimpl_->temp_bullets[0]);
525         return pimpl_->temp_bullets[index];
526 }
527
528
529 Bullet const & BufferParams::temp_bullet(lyx::size_type const index) const
530 {
531         LASSERT(index < 4, return pimpl_->temp_bullets[0]);
532         return pimpl_->temp_bullets[index];
533 }
534
535
536 Bullet & BufferParams::user_defined_bullet(lyx::size_type const index)
537 {
538         LASSERT(index < 4, return pimpl_->temp_bullets[0]);
539         return pimpl_->user_defined_bullets[index];
540 }
541
542
543 Bullet const & BufferParams::user_defined_bullet(lyx::size_type const index) const
544 {
545         LASSERT(index < 4, return pimpl_->temp_bullets[0]);
546         return pimpl_->user_defined_bullets[index];
547 }
548
549
550 Spacing & BufferParams::spacing()
551 {
552         return pimpl_->spacing;
553 }
554
555
556 Spacing const & BufferParams::spacing() const
557 {
558         return pimpl_->spacing;
559 }
560
561
562 PDFOptions & BufferParams::pdfoptions()
563 {
564         return pimpl_->pdfoptions;
565 }
566
567
568 PDFOptions const & BufferParams::pdfoptions() const
569 {
570         return pimpl_->pdfoptions;
571 }
572
573
574 HSpace const & BufferParams::getIndentation() const
575 {
576         return pimpl_->indentation;
577 }
578
579
580 void BufferParams::setIndentation(HSpace const & indent)
581 {
582         pimpl_->indentation = indent;
583 }
584
585
586 VSpace const & BufferParams::getDefSkip() const
587 {
588         return pimpl_->defskip;
589 }
590
591
592 void BufferParams::setDefSkip(VSpace const & vs)
593 {
594         // DEFSKIP will cause an infinite loop
595         LASSERT(vs.kind() != VSpace::DEFSKIP, return);
596         pimpl_->defskip = vs;
597 }
598
599
600 string BufferParams::readToken(Lexer & lex, string const & token,
601         FileName const & filepath)
602 {
603         if (token == "\\textclass") {
604                 lex.next();
605                 string const classname = lex.getString();
606                 // if there exists a local layout file, ignore the system one
607                 // NOTE: in this case, the textclass (.cls file) is assumed to
608                 // be available.
609                 string tcp;
610                 LayoutFileList & bcl = LayoutFileList::get();
611                 if (!filepath.empty())
612                         tcp = bcl.addLocalLayout(classname, filepath.absFileName());
613                 // that returns non-empty if a "local" layout file is found.
614                 if (!tcp.empty())
615                         setBaseClass(tcp);
616                 else
617                         setBaseClass(classname);
618                 // We assume that a tex class exists for local or unknown
619                 // layouts so this warning, will only be given for system layouts.
620                 if (!baseClass()->isTeXClassAvailable()) {
621                         docstring const desc =
622                                 translateIfPossible(from_utf8(baseClass()->description()));
623                         docstring const prereqs =
624                                 from_utf8(baseClass()->prerequisites());
625                         docstring const msg =
626                                 bformat(_("The selected document class\n"
627                                                  "\t%1$s\n"
628                                                  "requires external files that are not available.\n"
629                                                  "The document class can still be used, but the\n"
630                                                  "document cannot be compiled until the following\n"
631                                                  "prerequisites are installed:\n"
632                                                  "\t%2$s\n"
633                                                  "See section 3.1.2.2 (Class Availability) of the\n"
634                                                  "User's Guide for more information."), desc, prereqs);
635                         frontend::Alert::warning(_("Document class not available"),
636                                        msg);
637                 }
638         } else if (token == "\\begin_preamble") {
639                 readPreamble(lex);
640         } else if (token == "\\begin_local_layout") {
641                 readLocalLayout(lex, false);
642         } else if (token == "\\begin_forced_local_layout") {
643                 readLocalLayout(lex, true);
644         } else if (token == "\\begin_modules") {
645                 readModules(lex);
646         } else if (token == "\\begin_removed_modules") {
647                 readRemovedModules(lex);
648         } else if (token == "\\begin_includeonly") {
649                 readIncludeonly(lex);
650         } else if (token == "\\maintain_unincluded_children") {
651                 lex >> maintain_unincluded_children;
652         } else if (token == "\\options") {
653                 lex.eatLine();
654                 options = lex.getString();
655         } else if (token == "\\use_default_options") {
656                 lex >> use_default_options;
657         } else if (token == "\\master") {
658                 lex.eatLine();
659                 master = lex.getString();
660         } else if (token == "\\suppress_date") {
661                 lex >> suppress_date;
662         } else if (token == "\\justification") {
663                 lex >> justification;
664         } else if (token == "\\language") {
665                 readLanguage(lex);
666         } else if (token == "\\language_package") {
667                 lex.eatLine();
668                 lang_package = lex.getString();
669         } else if (token == "\\inputencoding") {
670                 lex >> inputenc;
671         } else if (token == "\\graphics") {
672                 readGraphicsDriver(lex);
673         } else if (token == "\\default_output_format") {
674                 lex >> default_output_format;
675         } else if (token == "\\bibtex_command") {
676                 lex.eatLine();
677                 bibtex_command = lex.getString();
678         } else if (token == "\\index_command") {
679                 lex.eatLine();
680                 index_command = lex.getString();
681         } else if (token == "\\fontencoding") {
682                 lex.eatLine();
683                 fontenc = lex.getString();
684         } else if (token == "\\font_roman") {
685                 lex.eatLine();
686                 fonts_roman = lex.getString();
687         } else if (token == "\\font_sans") {
688                 lex.eatLine();
689                 fonts_sans = lex.getString();
690         } else if (token == "\\font_typewriter") {
691                 lex.eatLine();
692                 fonts_typewriter = lex.getString();
693         } else if (token == "\\font_math") {
694                 lex.eatLine();
695                 fonts_math = lex.getString();
696         } else if (token == "\\font_default_family") {
697                 lex >> fonts_default_family;
698         } else if (token == "\\use_non_tex_fonts") {
699                 lex >> useNonTeXFonts;
700         } else if (token == "\\font_sc") {
701                 lex >> fonts_expert_sc;
702         } else if (token == "\\font_osf") {
703                 lex >> fonts_old_figures;
704         } else if (token == "\\font_sf_scale") {
705                 lex >> fonts_sans_scale;
706         } else if (token == "\\font_tt_scale") {
707                 lex >> fonts_typewriter_scale;
708         } else if (token == "\\font_cjk") {
709                 lex >> fonts_cjk;
710         } else if (token == "\\paragraph_separation") {
711                 string parsep;
712                 lex >> parsep;
713                 paragraph_separation = parseptranslator().find(parsep);
714         } else if (token == "\\paragraph_indentation") {
715                 lex.next();
716                 string indentation = lex.getString();
717                 pimpl_->indentation = HSpace(indentation);
718         } else if (token == "\\defskip") {
719                 lex.next();
720                 string const defskip = lex.getString();
721                 pimpl_->defskip = VSpace(defskip);
722                 if (pimpl_->defskip.kind() == VSpace::DEFSKIP)
723                         // that is invalid
724                         pimpl_->defskip = VSpace(VSpace::MEDSKIP);
725         } else if (token == "\\quotes_language") {
726                 string quotes_lang;
727                 lex >> quotes_lang;
728                 quotes_language = quoteslangtranslator().find(quotes_lang);
729         } else if (token == "\\papersize") {
730                 string ppsize;
731                 lex >> ppsize;
732                 papersize = papersizetranslator().find(ppsize);
733         } else if (token == "\\use_geometry") {
734                 lex >> use_geometry;
735         } else if (token == "\\use_package") {
736                 string package;
737                 int use;
738                 lex >> package;
739                 lex >> use;
740                 use_package(package, packagetranslator().find(use));
741         } else if (token == "\\cite_engine") {
742                 lex.eatLine();
743                 vector<string> engine = getVectorFromString(lex.getString());
744                 setCiteEngine(engine);
745         } else if (token == "\\cite_engine_type") {
746                 string engine_type;
747                 lex >> engine_type;
748                 cite_engine_type_ = citeenginetypetranslator().find(engine_type);
749         } else if (token == "\\biblio_style") {
750                 lex.eatLine();
751                 biblio_style = lex.getString();
752         } else if (token == "\\use_bibtopic") {
753                 lex >> use_bibtopic;
754         } else if (token == "\\use_indices") {
755                 lex >> use_indices;
756         } else if (token == "\\tracking_changes") {
757                 lex >> track_changes;
758         } else if (token == "\\output_changes") {
759                 lex >> output_changes;
760         } else if (token == "\\branch") {
761                 lex.eatLine();
762                 docstring branch = lex.getDocString();
763                 branchlist().add(branch);
764                 while (true) {
765                         lex.next();
766                         string const tok = lex.getString();
767                         if (tok == "\\end_branch")
768                                 break;
769                         Branch * branch_ptr = branchlist().find(branch);
770                         if (tok == "\\selected") {
771                                 lex.next();
772                                 if (branch_ptr)
773                                         branch_ptr->setSelected(lex.getInteger());
774                         }
775                         if (tok == "\\filename_suffix") {
776                                 lex.next();
777                                 if (branch_ptr)
778                                         branch_ptr->setFileNameSuffix(lex.getInteger());
779                         }
780                         if (tok == "\\color") {
781                                 lex.eatLine();
782                                 string color = lex.getString();
783                                 if (branch_ptr)
784                                         branch_ptr->setColor(color);
785                                 // Update also the Color table:
786                                 if (color == "none")
787                                         color = lcolor.getX11Name(Color_background);
788                                 // FIXME UNICODE
789                                 lcolor.setColor(to_utf8(branch), color);
790                         }
791                 }
792         } else if (token == "\\index") {
793                 lex.eatLine();
794                 docstring index = lex.getDocString();
795                 docstring shortcut;
796                 indiceslist().add(index);
797                 while (true) {
798                         lex.next();
799                         string const tok = lex.getString();
800                         if (tok == "\\end_index")
801                                 break;
802                         Index * index_ptr = indiceslist().find(index);
803                         if (tok == "\\shortcut") {
804                                 lex.next();
805                                 shortcut = lex.getDocString();
806                                 if (index_ptr)
807                                         index_ptr->setShortcut(shortcut);
808                         }
809                         if (tok == "\\color") {
810                                 lex.eatLine();
811                                 string color = lex.getString();
812                                 if (index_ptr)
813                                         index_ptr->setColor(color);
814                                 // Update also the Color table:
815                                 if (color == "none")
816                                         color = lcolor.getX11Name(Color_background);
817                                 // FIXME UNICODE
818                                 if (!shortcut.empty())
819                                         lcolor.setColor(to_utf8(shortcut), color);
820                         }
821                 }
822         } else if (token == "\\author") {
823                 lex.eatLine();
824                 istringstream ss(lex.getString());
825                 Author a;
826                 ss >> a;
827                 author_map[a.bufferId()] = pimpl_->authorlist.record(a);
828         } else if (token == "\\paperorientation") {
829                 string orient;
830                 lex >> orient;
831                 orientation = paperorientationtranslator().find(orient);
832         } else if (token == "\\backgroundcolor") {
833                 lex.eatLine();
834                 backgroundcolor = lyx::rgbFromHexName(lex.getString());
835                 isbackgroundcolor = true;
836         } else if (token == "\\fontcolor") {
837                 lex.eatLine();
838                 fontcolor = lyx::rgbFromHexName(lex.getString());
839                 isfontcolor = true;
840         } else if (token == "\\notefontcolor") {
841                 lex.eatLine();
842                 string color = lex.getString();
843                 notefontcolor = lyx::rgbFromHexName(color);
844                 lcolor.setColor("notefontcolor", color);
845         } else if (token == "\\boxbgcolor") {
846                 lex.eatLine();
847                 string color = lex.getString();
848                 boxbgcolor = lyx::rgbFromHexName(color);
849                 lcolor.setColor("boxbgcolor", color);
850         } else if (token == "\\paperwidth") {
851                 lex >> paperwidth;
852         } else if (token == "\\paperheight") {
853                 lex >> paperheight;
854         } else if (token == "\\leftmargin") {
855                 lex >> leftmargin;
856         } else if (token == "\\topmargin") {
857                 lex >> topmargin;
858         } else if (token == "\\rightmargin") {
859                 lex >> rightmargin;
860         } else if (token == "\\bottommargin") {
861                 lex >> bottommargin;
862         } else if (token == "\\headheight") {
863                 lex >> headheight;
864         } else if (token == "\\headsep") {
865                 lex >> headsep;
866         } else if (token == "\\footskip") {
867                 lex >> footskip;
868         } else if (token == "\\columnsep") {
869                 lex >> columnsep;
870         } else if (token == "\\paperfontsize") {
871                 lex >> fontsize;
872         } else if (token == "\\papercolumns") {
873                 lex >> columns;
874         } else if (token == "\\listings_params") {
875                 string par;
876                 lex >> par;
877                 listings_params = InsetListingsParams(par).params();
878         } else if (token == "\\papersides") {
879                 int psides;
880                 lex >> psides;
881                 sides = sidestranslator().find(psides);
882         } else if (token == "\\paperpagestyle") {
883                 lex >> pagestyle;
884         } else if (token == "\\bullet") {
885                 readBullets(lex);
886         } else if (token == "\\bulletLaTeX") {
887                 readBulletsLaTeX(lex);
888         } else if (token == "\\secnumdepth") {
889                 lex >> secnumdepth;
890         } else if (token == "\\tocdepth") {
891                 lex >> tocdepth;
892         } else if (token == "\\spacing") {
893                 string nspacing;
894                 lex >> nspacing;
895                 string tmp_val;
896                 if (nspacing == "other") {
897                         lex >> tmp_val;
898                 }
899                 spacing().set(spacetranslator().find(nspacing), tmp_val);
900         } else if (token == "\\float_placement") {
901                 lex >> float_placement;
902
903         } else if (prefixIs(token, "\\pdf_") || token == "\\use_hyperref") {
904                 string toktmp = pdfoptions().readToken(lex, token);
905                 if (!toktmp.empty()) {
906                         lyxerr << "PDFOptions::readToken(): Unknown token: " <<
907                                 toktmp << endl;
908                         return toktmp;
909                 }
910         } else if (token == "\\html_math_output") {
911                 int temp;
912                 lex >> temp;
913                 html_math_output = static_cast<MathOutput>(temp);
914         } else if (token == "\\html_be_strict") {
915                 lex >> html_be_strict;
916         } else if (token == "\\html_css_as_file") {
917                 lex >> html_css_as_file;
918         } else if (token == "\\html_math_img_scale") {
919                 lex >> html_math_img_scale;
920         } else if (token == "\\html_latex_start") {
921                 lex.eatLine();
922                 html_latex_start = lex.getString();
923         } else if (token == "\\html_latex_end") {
924                 lex.eatLine();
925                 html_latex_end = lex.getString();
926         } else if (token == "\\output_sync") {
927                 lex >> output_sync;
928         } else if (token == "\\output_sync_macro") {
929                 lex >> output_sync_macro;
930         } else if (token == "\\use_refstyle") {
931                 lex >> use_refstyle;
932         } else {
933                 lyxerr << "BufferParams::readToken(): Unknown token: " <<
934                         token << endl;
935                 return token;
936         }
937
938         return string();
939 }
940
941
942 void BufferParams::writeFile(ostream & os) const
943 {
944         // The top of the file is written by the buffer.
945         // Prints out the buffer info into the .lyx file given by file
946
947         // the textclass
948         os << "\\textclass " << baseClass()->name() << '\n';
949
950         // then the preamble
951         if (!preamble.empty()) {
952                 // remove '\n' from the end of preamble
953                 string const tmppreamble = rtrim(preamble, "\n");
954                 os << "\\begin_preamble\n"
955                    << tmppreamble
956                    << "\n\\end_preamble\n";
957         }
958
959         // the options
960         if (!options.empty()) {
961                 os << "\\options " << options << '\n';
962         }
963
964         // use the class options defined in the layout?
965         os << "\\use_default_options "
966            << convert<string>(use_default_options) << "\n";
967
968         // the master document
969         if (!master.empty()) {
970                 os << "\\master " << master << '\n';
971         }
972
973         // removed modules
974         if (!removed_modules_.empty()) {
975                 os << "\\begin_removed_modules" << '\n';
976                 list<string>::const_iterator it = removed_modules_.begin();
977                 list<string>::const_iterator en = removed_modules_.end();
978                 for (; it != en; ++it)
979                         os << *it << '\n';
980                 os << "\\end_removed_modules" << '\n';
981         }
982
983         // the modules
984         if (!layout_modules_.empty()) {
985                 os << "\\begin_modules" << '\n';
986                 LayoutModuleList::const_iterator it = layout_modules_.begin();
987                 LayoutModuleList::const_iterator en = layout_modules_.end();
988                 for (; it != en; ++it)
989                         os << *it << '\n';
990                 os << "\\end_modules" << '\n';
991         }
992
993         // includeonly
994         if (!included_children_.empty()) {
995                 os << "\\begin_includeonly" << '\n';
996                 list<string>::const_iterator it = included_children_.begin();
997                 list<string>::const_iterator en = included_children_.end();
998                 for (; it != en; ++it)
999                         os << *it << '\n';
1000                 os << "\\end_includeonly" << '\n';
1001         }
1002         os << "\\maintain_unincluded_children "
1003            << convert<string>(maintain_unincluded_children) << '\n';
1004
1005         // local layout information
1006         string const local_layout = getLocalLayout(false);
1007         if (!local_layout.empty()) {
1008                 // remove '\n' from the end
1009                 string const tmplocal = rtrim(local_layout, "\n");
1010                 os << "\\begin_local_layout\n"
1011                    << tmplocal
1012                    << "\n\\end_local_layout\n";
1013         }
1014         string const forced_local_layout = getLocalLayout(true);
1015         if (!forced_local_layout.empty()) {
1016                 // remove '\n' from the end
1017                 string const tmplocal = rtrim(forced_local_layout, "\n");
1018                 os << "\\begin_forced_local_layout\n"
1019                    << tmplocal
1020                    << "\n\\end_forced_local_layout\n";
1021         }
1022
1023         // then the text parameters
1024         if (language != ignore_language)
1025                 os << "\\language " << language->lang() << '\n';
1026         os << "\\language_package " << lang_package
1027            << "\n\\inputencoding " << inputenc
1028            << "\n\\fontencoding " << fontenc
1029            << "\n\\font_roman " << fonts_roman
1030            << "\n\\font_sans " << fonts_sans
1031            << "\n\\font_typewriter " << fonts_typewriter
1032            << "\n\\font_math " << fonts_math
1033            << "\n\\font_default_family " << fonts_default_family
1034            << "\n\\use_non_tex_fonts " << convert<string>(useNonTeXFonts)
1035            << "\n\\font_sc " << convert<string>(fonts_expert_sc)
1036            << "\n\\font_osf " << convert<string>(fonts_old_figures)
1037            << "\n\\font_sf_scale " << fonts_sans_scale
1038            << "\n\\font_tt_scale " << fonts_typewriter_scale
1039            << '\n';
1040         if (!fonts_cjk.empty()) {
1041                 os << "\\font_cjk " << fonts_cjk << '\n';
1042         }
1043         os << "\\graphics " << graphics_driver << '\n';
1044         os << "\\default_output_format " << default_output_format << '\n';
1045         os << "\\output_sync " << output_sync << '\n';
1046         if (!output_sync_macro.empty())
1047                 os << "\\output_sync_macro \"" << output_sync_macro << "\"\n";
1048         os << "\\bibtex_command " << bibtex_command << '\n';
1049         os << "\\index_command " << index_command << '\n';
1050
1051         if (!float_placement.empty()) {
1052                 os << "\\float_placement " << float_placement << '\n';
1053         }
1054         os << "\\paperfontsize " << fontsize << '\n';
1055
1056         spacing().writeFile(os);
1057         pdfoptions().writeFile(os);
1058
1059         os << "\\papersize " << string_papersize[papersize]
1060            << "\n\\use_geometry " << convert<string>(use_geometry);
1061         map<string, string> const & packages = auto_packages();
1062         for (map<string, string>::const_iterator it = packages.begin();
1063              it != packages.end(); ++it)
1064                 os << "\n\\use_package " << it->first << ' '
1065                    << use_package(it->first);
1066
1067         os << "\n\\cite_engine ";
1068
1069         if (!cite_engine_.empty()) {
1070                 LayoutModuleList::const_iterator be = cite_engine_.begin();
1071                 LayoutModuleList::const_iterator en = cite_engine_.end();
1072                 for (LayoutModuleList::const_iterator it = be; it != en; ++it) {
1073                         if (it != be)
1074                                 os << ',';
1075                         os << *it;
1076                 }
1077         } else {
1078                 os << "basic";
1079         }
1080
1081         os << "\n\\cite_engine_type " << citeenginetypetranslator().find(cite_engine_type_)
1082            << "\n\\biblio_style " << biblio_style
1083            << "\n\\use_bibtopic " << convert<string>(use_bibtopic)
1084            << "\n\\use_indices " << convert<string>(use_indices)
1085            << "\n\\paperorientation " << string_orientation[orientation]
1086            << "\n\\suppress_date " << convert<string>(suppress_date)
1087            << "\n\\justification " << convert<string>(justification)
1088            << "\n\\use_refstyle " << use_refstyle
1089            << '\n';
1090         if (isbackgroundcolor == true)
1091                 os << "\\backgroundcolor " << lyx::X11hexname(backgroundcolor) << '\n';
1092         if (isfontcolor == true)
1093                 os << "\\fontcolor " << lyx::X11hexname(fontcolor) << '\n';
1094         if (notefontcolor != lyx::rgbFromHexName("#cccccc"))
1095                 os << "\\notefontcolor " << lyx::X11hexname(notefontcolor) << '\n';
1096         if (boxbgcolor != lyx::rgbFromHexName("#ff0000"))
1097                 os << "\\boxbgcolor " << lyx::X11hexname(boxbgcolor) << '\n';
1098
1099         BranchList::const_iterator it = branchlist().begin();
1100         BranchList::const_iterator end = branchlist().end();
1101         for (; it != end; ++it) {
1102                 os << "\\branch " << to_utf8(it->branch())
1103                    << "\n\\selected " << it->isSelected()
1104                    << "\n\\filename_suffix " << it->hasFileNameSuffix()
1105                    << "\n\\color " << lyx::X11hexname(it->color())
1106                    << "\n\\end_branch"
1107                    << "\n";
1108         }
1109
1110         IndicesList::const_iterator iit = indiceslist().begin();
1111         IndicesList::const_iterator iend = indiceslist().end();
1112         for (; iit != iend; ++iit) {
1113                 os << "\\index " << to_utf8(iit->index())
1114                    << "\n\\shortcut " << to_utf8(iit->shortcut())
1115                    << "\n\\color " << lyx::X11hexname(iit->color())
1116                    << "\n\\end_index"
1117                    << "\n";
1118         }
1119
1120         if (!paperwidth.empty())
1121                 os << "\\paperwidth "
1122                    << VSpace(paperwidth).asLyXCommand() << '\n';
1123         if (!paperheight.empty())
1124                 os << "\\paperheight "
1125                    << VSpace(paperheight).asLyXCommand() << '\n';
1126         if (!leftmargin.empty())
1127                 os << "\\leftmargin "
1128                    << VSpace(leftmargin).asLyXCommand() << '\n';
1129         if (!topmargin.empty())
1130                 os << "\\topmargin "
1131                    << VSpace(topmargin).asLyXCommand() << '\n';
1132         if (!rightmargin.empty())
1133                 os << "\\rightmargin "
1134                    << VSpace(rightmargin).asLyXCommand() << '\n';
1135         if (!bottommargin.empty())
1136                 os << "\\bottommargin "
1137                    << VSpace(bottommargin).asLyXCommand() << '\n';
1138         if (!headheight.empty())
1139                 os << "\\headheight "
1140                    << VSpace(headheight).asLyXCommand() << '\n';
1141         if (!headsep.empty())
1142                 os << "\\headsep "
1143                    << VSpace(headsep).asLyXCommand() << '\n';
1144         if (!footskip.empty())
1145                 os << "\\footskip "
1146                    << VSpace(footskip).asLyXCommand() << '\n';
1147         if (!columnsep.empty())
1148                 os << "\\columnsep "
1149                          << VSpace(columnsep).asLyXCommand() << '\n';
1150         os << "\\secnumdepth " << secnumdepth
1151            << "\n\\tocdepth " << tocdepth
1152            << "\n\\paragraph_separation "
1153            << string_paragraph_separation[paragraph_separation];
1154         if (!paragraph_separation)
1155                 os << "\n\\paragraph_indentation " << getIndentation().asLyXCommand();
1156         else
1157                 os << "\n\\defskip " << getDefSkip().asLyXCommand();
1158         os << "\n\\quotes_language "
1159            << string_quotes_language[quotes_language]
1160            << "\n\\papercolumns " << columns
1161            << "\n\\papersides " << sides
1162            << "\n\\paperpagestyle " << pagestyle << '\n';
1163         if (!listings_params.empty())
1164                 os << "\\listings_params \"" <<
1165                         InsetListingsParams(listings_params).encodedString() << "\"\n";
1166         for (int i = 0; i < 4; ++i) {
1167                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1168                         if (user_defined_bullet(i).getFont() != -1) {
1169                                 os << "\\bullet " << i << " "
1170                                    << user_defined_bullet(i).getFont() << " "
1171                                    << user_defined_bullet(i).getCharacter() << " "
1172                                    << user_defined_bullet(i).getSize() << "\n";
1173                         }
1174                         else {
1175                                 // FIXME UNICODE
1176                                 os << "\\bulletLaTeX " << i << " \""
1177                                    << lyx::to_ascii(user_defined_bullet(i).getText())
1178                                    << "\"\n";
1179                         }
1180                 }
1181         }
1182
1183         os << "\\tracking_changes " << convert<string>(track_changes) << '\n'
1184            << "\\output_changes " << convert<string>(output_changes) << '\n'
1185            << "\\html_math_output " << html_math_output << '\n'
1186            << "\\html_css_as_file " << html_css_as_file << '\n'
1187            << "\\html_be_strict " << convert<string>(html_be_strict) << '\n';
1188
1189         if (html_math_img_scale != 1.0)
1190                 os << "\\html_math_img_scale " << convert<string>(html_math_img_scale) << '\n';
1191         if (!html_latex_start.empty())
1192                 os << "\\html_latex_start " << html_latex_start << '\n';
1193         if (!html_latex_end.empty())
1194                  os << "\\html_latex_end " << html_latex_end << '\n';
1195
1196         os << pimpl_->authorlist;
1197 }
1198
1199
1200 void BufferParams::validate(LaTeXFeatures & features) const
1201 {
1202         features.require(documentClass().requires());
1203
1204         if (columns > 1 && language->rightToLeft())
1205                 features.require("rtloutputdblcol");
1206
1207         if (output_changes) {
1208                 bool dvipost    = LaTeXFeatures::isAvailable("dvipost");
1209                 bool xcolorulem = LaTeXFeatures::isAvailable("ulem") &&
1210                                   LaTeXFeatures::isAvailable("xcolor");
1211
1212                 switch (features.runparams().flavor) {
1213                 case OutputParams::LATEX:
1214                 case OutputParams::DVILUATEX:
1215                         if (dvipost) {
1216                                 features.require("ct-dvipost");
1217                                 features.require("dvipost");
1218                         } else if (xcolorulem) {
1219                                 features.require("ct-xcolor-ulem");
1220                                 features.require("ulem");
1221                                 features.require("xcolor");
1222                         } else {
1223                                 features.require("ct-none");
1224                         }
1225                         break;
1226                 case OutputParams::LUATEX:
1227                 case OutputParams::PDFLATEX:
1228                 case OutputParams::XETEX:
1229                         if (xcolorulem) {
1230                                 features.require("ct-xcolor-ulem");
1231                                 features.require("ulem");
1232                                 features.require("xcolor");
1233                                 // improves color handling in PDF output
1234                                 features.require("pdfcolmk");
1235                         } else {
1236                                 features.require("ct-none");
1237                         }
1238                         break;
1239                 default:
1240                         break;
1241                 }
1242         }
1243
1244         // Floats with 'Here definitely' as default setting.
1245         if (float_placement.find('H') != string::npos)
1246                 features.require("float");
1247
1248         for (PackageMap::const_iterator it = use_packages.begin();
1249              it != use_packages.end(); ++it) {
1250                 if (it->first == "amsmath") {
1251                         // AMS Style is at document level
1252                         if (it->second == package_on ||
1253                             features.isProvided("amsmath"))
1254                                 features.require(it->first);
1255                 } else if (it->second == package_on)
1256                         features.require(it->first);
1257         }
1258
1259         // Document-level line spacing
1260         if (spacing().getSpace() != Spacing::Single && !spacing().isDefault())
1261                 features.require("setspace");
1262
1263         // the bullet shapes are buffer level not paragraph level
1264         // so they are tested here
1265         for (int i = 0; i < 4; ++i) {
1266                 if (user_defined_bullet(i) == ITEMIZE_DEFAULTS[i])
1267                         continue;
1268                 int const font = user_defined_bullet(i).getFont();
1269                 if (font == 0) {
1270                         int const c = user_defined_bullet(i).getCharacter();
1271                         if (c == 16
1272                             || c == 17
1273                             || c == 25
1274                             || c == 26
1275                             || c == 31) {
1276                                 features.require("latexsym");
1277                         }
1278                 } else if (font == 1) {
1279                         features.require("amssymb");
1280                 } else if (font >= 2 && font <= 5) {
1281                         features.require("pifont");
1282                 }
1283         }
1284
1285         if (pdfoptions().use_hyperref) {
1286                 features.require("hyperref");
1287                 // due to interferences with babel and hyperref, the color package has to
1288                 // be loaded after hyperref when hyperref is used with the colorlinks
1289                 // option, see http://www.lyx.org/trac/ticket/5291
1290                 if (pdfoptions().colorlinks)
1291                         features.require("color");
1292         }
1293         if (!listings_params.empty()) {
1294                 // do not test validity because listings_params is
1295                 // supposed to be valid
1296                 string par =
1297                         InsetListingsParams(listings_params).separatedParams(true);
1298                 // we can't support all packages, but we should load the color package
1299                 if (par.find("\\color", 0) != string::npos)
1300                         features.require("color");
1301         }
1302
1303         // some languages are only available via polyglossia
1304         if (features.runparams().flavor == OutputParams::XETEX
1305             && (features.hasPolyglossiaExclusiveLanguages()
1306                 || useNonTeXFonts))
1307                 features.require("polyglossia");
1308
1309         if (useNonTeXFonts && fonts_math != "auto")
1310                 features.require("unicode-math");
1311
1312         if (!language->requires().empty())
1313                 features.require(language->requires());
1314 }
1315
1316
1317 bool BufferParams::writeLaTeX(otexstream & os, LaTeXFeatures & features,
1318                               FileName const & filepath) const
1319 {
1320         // http://www.tug.org/texmf-dist/doc/latex/base/fixltx2e.pdf
1321         // !! To use the Fix-cm package, load it before \documentclass, and use the command
1322         // \RequirePackage to do so, rather than the normal \usepackage
1323         // Do not try to load any other package before the document class, unless you
1324         // have a thorough understanding of the LATEX internals and know exactly what you
1325         // are doing!
1326         if (features.mustProvide("fix-cm"))
1327                 os << "\\RequirePackage{fix-cm}\n";
1328
1329         os << "\\documentclass";
1330
1331         DocumentClass const & tclass = documentClass();
1332
1333         ostringstream clsoptions; // the document class options.
1334
1335         if (tokenPos(tclass.opt_fontsize(),
1336                      '|', fontsize) >= 0) {
1337                 // only write if existing in list (and not default)
1338                 clsoptions << fontsize << "pt,";
1339         }
1340
1341         // all paper sizes except of A4, A5, B5 and the US sizes need the
1342         // geometry package
1343         bool nonstandard_papersize = papersize != PAPER_DEFAULT
1344                 && papersize != PAPER_USLETTER
1345                 && papersize != PAPER_USLEGAL
1346                 && papersize != PAPER_USEXECUTIVE
1347                 && papersize != PAPER_A4
1348                 && papersize != PAPER_A5
1349                 && papersize != PAPER_B5;
1350
1351         if (!use_geometry) {
1352                 switch (papersize) {
1353                 case PAPER_A4:
1354                         clsoptions << "a4paper,";
1355                         break;
1356                 case PAPER_USLETTER:
1357                         clsoptions << "letterpaper,";
1358                         break;
1359                 case PAPER_A5:
1360                         clsoptions << "a5paper,";
1361                         break;
1362                 case PAPER_B5:
1363                         clsoptions << "b5paper,";
1364                         break;
1365                 case PAPER_USEXECUTIVE:
1366                         clsoptions << "executivepaper,";
1367                         break;
1368                 case PAPER_USLEGAL:
1369                         clsoptions << "legalpaper,";
1370                         break;
1371                 case PAPER_DEFAULT:
1372                 case PAPER_A0:
1373                 case PAPER_A1:
1374                 case PAPER_A2:
1375                 case PAPER_A3:
1376                 case PAPER_A6:
1377                 case PAPER_B0:
1378                 case PAPER_B1:
1379                 case PAPER_B2:
1380                 case PAPER_B3:
1381                 case PAPER_B4:
1382                 case PAPER_B6:
1383                 case PAPER_C0:
1384                 case PAPER_C1:
1385                 case PAPER_C2:
1386                 case PAPER_C3:
1387                 case PAPER_C4:
1388                 case PAPER_C5:
1389                 case PAPER_C6:
1390                 case PAPER_JISB0:
1391                 case PAPER_JISB1:
1392                 case PAPER_JISB2:
1393                 case PAPER_JISB3:
1394                 case PAPER_JISB4:
1395                 case PAPER_JISB5:
1396                 case PAPER_JISB6:
1397                 case PAPER_CUSTOM:
1398                         break;
1399                 }
1400         }
1401
1402         // if needed
1403         if (sides != tclass.sides()) {
1404                 switch (sides) {
1405                 case OneSide:
1406                         clsoptions << "oneside,";
1407                         break;
1408                 case TwoSides:
1409                         clsoptions << "twoside,";
1410                         break;
1411                 }
1412         }
1413
1414         // if needed
1415         if (columns != tclass.columns()) {
1416                 if (columns == 2)
1417                         clsoptions << "twocolumn,";
1418                 else
1419                         clsoptions << "onecolumn,";
1420         }
1421
1422         if (!use_geometry
1423             && orientation == ORIENTATION_LANDSCAPE)
1424                 clsoptions << "landscape,";
1425
1426         // language should be a parameter to \documentclass
1427         if (language->babel() == "hebrew"
1428             && default_language->babel() != "hebrew")
1429                 // This seems necessary
1430                 features.useLanguage(default_language);
1431
1432         ostringstream language_options;
1433         bool const use_babel = features.useBabel() && !features.isProvided("babel");
1434         bool const use_polyglossia = features.usePolyglossia();
1435         bool const global = lyxrc.language_global_options;
1436         if (use_babel || (use_polyglossia && global)) {
1437                 language_options << features.getBabelLanguages();
1438                 if (!language->babel().empty()) {
1439                         if (!language_options.str().empty())
1440                                 language_options << ',';
1441                         language_options << language->babel();
1442                 }
1443                 if (global && !features.needBabelLangOptions()
1444                     && !language_options.str().empty())
1445                         clsoptions << language_options.str() << ',';
1446         }
1447
1448         // the predefined options from the layout
1449         if (use_default_options && !tclass.options().empty())
1450                 clsoptions << tclass.options() << ',';
1451
1452         // the user-defined options
1453         if (!options.empty()) {
1454                 clsoptions << options << ',';
1455         }
1456
1457         string strOptions(clsoptions.str());
1458         if (!strOptions.empty()) {
1459                 strOptions = rtrim(strOptions, ",");
1460                 // FIXME UNICODE
1461                 os << '[' << from_utf8(strOptions) << ']';
1462         }
1463
1464         os << '{' << from_ascii(tclass.latexname()) << "}\n";
1465         // end of \documentclass defs
1466
1467         // if we use fontspec or newtxmath, we have to load the AMS packages here
1468         string const ams = features.loadAMSPackages();
1469         bool const ot1 = (font_encoding() == "default" || font_encoding() == "OT1");
1470         bool const use_newtxmath =
1471                 theLaTeXFonts().getLaTeXFont(from_ascii(fonts_math)).getUsedPackage(
1472                         ot1, false, false) == "newtxmath";
1473         if ((useNonTeXFonts || use_newtxmath) && !ams.empty())
1474                 os << from_ascii(ams);
1475
1476         if (useNonTeXFonts) {
1477                 os << "\\usepackage{fontspec}\n";
1478                 if (features.mustProvide("unicode-math")
1479                     && features.isAvailable("unicode-math"))
1480                         os << "\\usepackage{unicode-math}\n";
1481         }
1482
1483         // font selection must be done before loading fontenc.sty
1484         string const fonts = loadFonts(features);
1485         if (!fonts.empty())
1486                 os << from_utf8(fonts);
1487
1488         if (fonts_default_family != "default")
1489                 os << "\\renewcommand{\\familydefault}{\\"
1490                    << from_ascii(fonts_default_family) << "}\n";
1491
1492         // set font encoding
1493         // for arabic_arabi and farsi we also need to load the LAE and
1494         // LFE encoding
1495         // XeTeX and LuaTeX (with OS fonts) work without fontenc
1496         if (font_encoding() != "default" && language->lang() != "japanese"
1497             && !useNonTeXFonts && !features.isProvided("fontenc")) {
1498                 docstring extra_encoding;
1499                 if (features.mustProvide("textgreek"))
1500                         extra_encoding += from_ascii("LGR");
1501                 if (features.mustProvide("textcyr")) {
1502                         if (!extra_encoding.empty())
1503                                 extra_encoding.push_back(',');
1504                         extra_encoding += from_ascii("T2A");
1505                 }
1506                 if (!extra_encoding.empty() && !font_encoding().empty())
1507                         extra_encoding.push_back(',');
1508                 size_t fars = language_options.str().find("farsi");
1509                 size_t arab = language_options.str().find("arabic");
1510                 if (language->lang() == "arabic_arabi"
1511                         || language->lang() == "farsi" || fars != string::npos
1512                         || arab != string::npos) {
1513                         os << "\\usepackage[" << extra_encoding
1514                            << from_ascii(font_encoding())
1515                            << ",LFE,LAE]{fontenc}\n";
1516                 } else {
1517                         os << "\\usepackage[" << extra_encoding
1518                            << from_ascii(font_encoding())
1519                            << "]{fontenc}\n";
1520                 }
1521         }
1522
1523         // handle inputenc etc.
1524         writeEncodingPreamble(os, features);
1525
1526         // includeonly
1527         if (!features.runparams().includeall && !included_children_.empty()) {
1528                 os << "\\includeonly{";
1529                 list<string>::const_iterator it = included_children_.begin();
1530                 list<string>::const_iterator en = included_children_.end();
1531                 bool first = true;
1532                 for (; it != en; ++it) {
1533                         string incfile = *it;
1534                         FileName inc = makeAbsPath(incfile, filepath.absFileName());
1535                         string mangled = DocFileName(changeExtension(inc.absFileName(), ".tex")).
1536                         mangledFileName();
1537                         if (!features.runparams().nice)
1538                                 incfile = mangled;
1539                         // \includeonly doesn't want an extension
1540                         incfile = changeExtension(incfile, string());
1541                         incfile = support::latex_path(incfile);
1542                         if (!incfile.empty()) {
1543                                 if (!first)
1544                                         os << ",";
1545                                 os << from_utf8(incfile);
1546                         }
1547                         first = false;
1548                 }
1549                 os << "}\n";
1550         }
1551
1552         if (!features.isProvided("geometry")
1553             && (use_geometry || nonstandard_papersize)) {
1554                 odocstringstream ods;
1555                 if (!getGraphicsDriver("geometry").empty())
1556                         ods << getGraphicsDriver("geometry");
1557                 if (orientation == ORIENTATION_LANDSCAPE)
1558                         ods << ",landscape";
1559                 switch (papersize) {
1560                 case PAPER_CUSTOM:
1561                         if (!paperwidth.empty())
1562                                 ods << ",paperwidth="
1563                                    << from_ascii(paperwidth);
1564                         if (!paperheight.empty())
1565                                 ods << ",paperheight="
1566                                    << from_ascii(paperheight);
1567                         break;
1568                 case PAPER_USLETTER:
1569                         ods << ",letterpaper";
1570                         break;
1571                 case PAPER_USLEGAL:
1572                         ods << ",legalpaper";
1573                         break;
1574                 case PAPER_USEXECUTIVE:
1575                         ods << ",executivepaper";
1576                         break;
1577                 case PAPER_A0:
1578                         ods << ",a0paper";
1579                         break;
1580                 case PAPER_A1:
1581                         ods << ",a1paper";
1582                         break;
1583                 case PAPER_A2:
1584                         ods << ",a2paper";
1585                         break;
1586                 case PAPER_A3:
1587                         ods << ",a3paper";
1588                         break;
1589                 case PAPER_A4:
1590                         ods << ",a4paper";
1591                         break;
1592                 case PAPER_A5:
1593                         ods << ",a5paper";
1594                         break;
1595                 case PAPER_A6:
1596                         ods << ",a6paper";
1597                         break;
1598                 case PAPER_B0:
1599                         ods << ",b0paper";
1600                         break;
1601                 case PAPER_B1:
1602                         ods << ",b1paper";
1603                         break;
1604                 case PAPER_B2:
1605                         ods << ",b2paper";
1606                         break;
1607                 case PAPER_B3:
1608                         ods << ",b3paper";
1609                         break;
1610                 case PAPER_B4:
1611                         ods << ",b4paper";
1612                         break;
1613                 case PAPER_B5:
1614                         ods << ",b5paper";
1615                         break;
1616                 case PAPER_B6:
1617                         ods << ",b6paper";
1618                         break;
1619                 case PAPER_C0:
1620                         ods << ",c0paper";
1621                         break;
1622                 case PAPER_C1:
1623                         ods << ",c1paper";
1624                         break;
1625                 case PAPER_C2:
1626                         ods << ",c2paper";
1627                         break;
1628                 case PAPER_C3:
1629                         ods << ",c3paper";
1630                         break;
1631                 case PAPER_C4:
1632                         ods << ",c4paper";
1633                         break;
1634                 case PAPER_C5:
1635                         ods << ",c5paper";
1636                         break;
1637                 case PAPER_C6:
1638                         ods << ",c6paper";
1639                         break;
1640                 case PAPER_JISB0:
1641                         ods << ",b0j";
1642                         break;
1643                 case PAPER_JISB1:
1644                         ods << ",b1j";
1645                         break;
1646                 case PAPER_JISB2:
1647                         ods << ",b2j";
1648                         break;
1649                 case PAPER_JISB3:
1650                         ods << ",b3j";
1651                         break;
1652                 case PAPER_JISB4:
1653                         ods << ",b4j";
1654                         break;
1655                 case PAPER_JISB5:
1656                         ods << ",b5j";
1657                         break;
1658                 case PAPER_JISB6:
1659                         ods << ",b6j";
1660                         break;
1661                 case PAPER_DEFAULT:
1662                         break;
1663                 }
1664                 docstring const g_options = trim(ods.str(), ",");
1665                 os << "\\usepackage";
1666                 if (!g_options.empty())
1667                         os << '[' << g_options << ']';
1668                 os << "{geometry}\n";
1669                 // output this only if use_geometry is true
1670                 if (use_geometry) {
1671                         os << "\\geometry{verbose";
1672                         if (!topmargin.empty())
1673                                 os << ",tmargin=" << from_ascii(Length(topmargin).asLatexString());
1674                         if (!bottommargin.empty())
1675                                 os << ",bmargin=" << from_ascii(Length(bottommargin).asLatexString());
1676                         if (!leftmargin.empty())
1677                                 os << ",lmargin=" << from_ascii(Length(leftmargin).asLatexString());
1678                         if (!rightmargin.empty())
1679                                 os << ",rmargin=" << from_ascii(Length(rightmargin).asLatexString());
1680                         if (!headheight.empty())
1681                                 os << ",headheight=" << from_ascii(Length(headheight).asLatexString());
1682                         if (!headsep.empty())
1683                                 os << ",headsep=" << from_ascii(Length(headsep).asLatexString());
1684                         if (!footskip.empty())
1685                                 os << ",footskip=" << from_ascii(Length(footskip).asLatexString());
1686                         if (!columnsep.empty())
1687                                 os << ",columnsep=" << from_ascii(Length(columnsep).asLatexString());
1688                         os << "}\n";
1689                 }
1690         } else if (orientation == ORIENTATION_LANDSCAPE
1691                    || papersize != PAPER_DEFAULT) {
1692                 features.require("papersize");
1693         }
1694
1695         if (tokenPos(tclass.opt_pagestyle(), '|', pagestyle) >= 0) {
1696                 if (pagestyle == "fancy")
1697                         os << "\\usepackage{fancyhdr}\n";
1698                 os << "\\pagestyle{" << from_ascii(pagestyle) << "}\n";
1699         }
1700
1701         // only output when the background color is not default
1702         if (isbackgroundcolor == true) {
1703                 // only require color here, the background color will be defined
1704                 // in LaTeXFeatures.cpp to avoid interferences with the LaTeX
1705                 // package pdfpages
1706                 features.require("color");
1707                 features.require("pagecolor");
1708         }
1709
1710         // only output when the font color is not default
1711         if (isfontcolor == true) {
1712                 // only require color here, the font color will be defined
1713                 // in LaTeXFeatures.cpp to avoid interferences with the LaTeX
1714                 // package pdfpages
1715                 features.require("color");
1716                 features.require("fontcolor");
1717         }
1718
1719         // Only if class has a ToC hierarchy
1720         if (tclass.hasTocLevels()) {
1721                 if (secnumdepth != tclass.secnumdepth()) {
1722                         os << "\\setcounter{secnumdepth}{"
1723                            << secnumdepth
1724                            << "}\n";
1725                 }
1726                 if (tocdepth != tclass.tocdepth()) {
1727                         os << "\\setcounter{tocdepth}{"
1728                            << tocdepth
1729                            << "}\n";
1730                 }
1731         }
1732
1733         if (paragraph_separation) {
1734                 // when skip separation
1735                 switch (getDefSkip().kind()) {
1736                 case VSpace::SMALLSKIP:
1737                         os << "\\setlength{\\parskip}{\\smallskipamount}\n";
1738                         break;
1739                 case VSpace::MEDSKIP:
1740                         os << "\\setlength{\\parskip}{\\medskipamount}\n";
1741                         break;
1742                 case VSpace::BIGSKIP:
1743                         os << "\\setlength{\\parskip}{\\bigskipamount}\n";
1744                         break;
1745                 case VSpace::LENGTH:
1746                         os << "\\setlength{\\parskip}{"
1747                            << from_utf8(getDefSkip().length().asLatexString())
1748                            << "}\n";
1749                         break;
1750                 default: // should never happen // Then delete it.
1751                         os << "\\setlength{\\parskip}{\\medskipamount}\n";
1752                         break;
1753                 }
1754                 os << "\\setlength{\\parindent}{0pt}\n";
1755         } else {
1756                 // when separation by indentation
1757                 // only output something when a width is given
1758                 if (getIndentation().asLyXCommand() != "default") {
1759                         os << "\\setlength{\\parindent}{"
1760                            << from_utf8(getIndentation().asLatexCommand())
1761                            << "}\n";
1762                 }
1763         }
1764
1765         // Now insert the LyX specific LaTeX commands...
1766         docstring lyxpreamble;
1767         features.resolveAlternatives();
1768
1769         if (output_sync) {
1770                 if (!output_sync_macro.empty())
1771                         lyxpreamble += from_utf8(output_sync_macro) +"\n";
1772                 else if (features.runparams().flavor == OutputParams::LATEX)
1773                         lyxpreamble += "\\usepackage[active]{srcltx}\n";
1774                 else if (features.runparams().flavor == OutputParams::PDFLATEX)
1775                         lyxpreamble += "\\synctex=-1\n";
1776         }
1777
1778         // due to interferences with babel and hyperref, the color package has to
1779         // be loaded (when it is not already loaded) before babel when hyperref
1780         // is used with the colorlinks option, see
1781         // http://www.lyx.org/trac/ticket/5291
1782         // we decided therefore to load color always before babel, see
1783         // http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg144349.html
1784         lyxpreamble += from_ascii(features.getColorOptions());
1785
1786         // If we use hyperref, jurabib, japanese, varioref or vietnamese,
1787         // we have to call babel before
1788         if (use_babel
1789             && (features.isRequired("jurabib")
1790                 || features.isRequired("hyperref")
1791                 || features.isRequired("varioref")
1792                 || features.isRequired("vietnamese")
1793                 || features.isRequired("japanese"))) {
1794                         // FIXME UNICODE
1795                         lyxpreamble += from_utf8(features.getBabelPresettings());
1796                         lyxpreamble += from_utf8(babelCall(language_options.str(),
1797                                                            features.needBabelLangOptions())) + '\n';
1798                         lyxpreamble += from_utf8(features.getBabelPostsettings());
1799         }
1800
1801         // The optional packages;
1802         lyxpreamble += from_ascii(features.getPackages());
1803
1804         // Additional Indices
1805         if (features.isRequired("splitidx")) {
1806                 IndicesList::const_iterator iit = indiceslist().begin();
1807                 IndicesList::const_iterator iend = indiceslist().end();
1808                 for (; iit != iend; ++iit) {
1809                         pair<docstring, docstring> indexname_latex =
1810                                 features.runparams().encoding->latexString(iit->index(),
1811                                                                            features.runparams().dryrun);
1812                         if (!indexname_latex.second.empty()) {
1813                                 // issue a warning about omitted characters
1814                                 // FIXME: should be passed to the error dialog
1815                                 frontend::Alert::warning(_("Uncodable characters"),
1816                                         bformat(_("The following characters that are used in an index name are not\n"
1817                                                   "representable in the current encoding and therefore have been omitted:\n%1$s."),
1818                                                 indexname_latex.second));
1819                         }
1820                         lyxpreamble += "\\newindex[";
1821                         lyxpreamble += indexname_latex.first;
1822                         lyxpreamble += "]{";
1823                         lyxpreamble += escape(iit->shortcut());
1824                         lyxpreamble += "}\n";
1825                 }
1826         }
1827
1828         // Line spacing
1829         lyxpreamble += from_utf8(spacing().writePreamble(features.isProvided("SetSpace")));
1830
1831         // PDF support.
1832         // * Hyperref manual: "Make sure it comes last of your loaded
1833         //   packages, to give it a fighting chance of not being over-written,
1834         //   since its job is to redefine many LaTeX commands."
1835         // * Email from Heiko Oberdiek: "It is usually better to load babel
1836         //   before hyperref. Then hyperref has a chance to detect babel.
1837         // * Has to be loaded before the "LyX specific LaTeX commands" to
1838         //   avoid errors with algorithm floats.
1839         // use hyperref explicitly if it is required
1840         if (features.isRequired("hyperref")) {
1841                 // pass what we have to stream here, since we need
1842                 // to access the stream itself in PDFOptions.
1843                 os << lyxpreamble;
1844
1845                 OutputParams tmp_params = features.runparams();
1846                 pdfoptions().writeLaTeX(tmp_params, os,
1847                                         features.isProvided("hyperref"));
1848                 // set back for the rest
1849                 lyxpreamble.clear();
1850                 // correctly break URLs with hyperref and dvi output
1851                 if (features.runparams().flavor == OutputParams::LATEX
1852                     && features.isAvailable("breakurl"))
1853                         lyxpreamble += "\\usepackage{breakurl}\n";
1854         } else if (features.isRequired("nameref"))
1855                 // hyperref loads this automatically
1856                 lyxpreamble += "\\usepackage{nameref}\n";
1857
1858         // bibtopic needs to be loaded after hyperref.
1859         // the dot provides the aux file naming which LyX can detect.
1860         if (features.mustProvide("bibtopic"))
1861                 lyxpreamble += "\\usepackage[dot]{bibtopic}\n";
1862
1863         // Will be surrounded by \makeatletter and \makeatother when not empty
1864         docstring atlyxpreamble;
1865
1866         // Some macros LyX will need
1867         docstring tmppreamble(features.getMacros());
1868
1869         if (!tmppreamble.empty())
1870                 atlyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1871                         "LyX specific LaTeX commands.\n"
1872                         + tmppreamble + '\n';
1873
1874         // the text class specific preamble
1875         tmppreamble = features.getTClassPreamble();
1876         if (!tmppreamble.empty())
1877                 atlyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1878                         "Textclass specific LaTeX commands.\n"
1879                         + tmppreamble + '\n';
1880
1881         // suppress date if selected
1882         // use \@ifundefined because we cannot be sure that every document class
1883         // has a \date command
1884         if (suppress_date)
1885                 atlyxpreamble += "\\@ifundefined{date}{}{\\date{}}\n";
1886
1887         /* the user-defined preamble */
1888         if (!containsOnly(preamble, " \n\t"))
1889                 // FIXME UNICODE
1890                 atlyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1891                         "User specified LaTeX commands.\n"
1892                         + from_utf8(preamble) + '\n';
1893
1894         // subfig loads internally the LaTeX package "caption". As
1895         // caption is a very popular package, users will load it in
1896         // the preamble. Therefore we must load subfig behind the
1897         // user-defined preamble and check if the caption package was
1898         // loaded or not. For the case that caption is loaded before
1899         // subfig, there is the subfig option "caption=false". This
1900         // option also works when a koma-script class is used and
1901         // koma's own caption commands are used instead of caption. We
1902         // use \PassOptionsToPackage here because the user could have
1903         // already loaded subfig in the preamble.
1904         if (features.isRequired("subfig")) {
1905                 atlyxpreamble += "\\@ifundefined{showcaptionsetup}{}{%\n"
1906                         " \\PassOptionsToPackage{caption=false}{subfig}}\n"
1907                         "\\usepackage{subfig}\n";
1908         }
1909
1910         // Itemize bullet settings need to be last in case the user
1911         // defines their own bullets that use a package included
1912         // in the user-defined preamble -- ARRae
1913         // Actually it has to be done much later than that
1914         // since some packages like frenchb make modifications
1915         // at \begin{document} time -- JMarc
1916         docstring bullets_def;
1917         for (int i = 0; i < 4; ++i) {
1918                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1919                         if (bullets_def.empty())
1920                                 bullets_def += "\\AtBeginDocument{\n";
1921                         bullets_def += "  \\def\\labelitemi";
1922                         switch (i) {
1923                                 // `i' is one less than the item to modify
1924                         case 0:
1925                                 break;
1926                         case 1:
1927                                 bullets_def += 'i';
1928                                 break;
1929                         case 2:
1930                                 bullets_def += "ii";
1931                                 break;
1932                         case 3:
1933                                 bullets_def += 'v';
1934                                 break;
1935                         }
1936                         bullets_def += '{' +
1937                                 user_defined_bullet(i).getText()
1938                                 + "}\n";
1939                 }
1940         }
1941
1942         if (!bullets_def.empty())
1943                 atlyxpreamble += bullets_def + "}\n\n";
1944
1945         if (!atlyxpreamble.empty())
1946                 lyxpreamble += "\n\\makeatletter\n"
1947                         + atlyxpreamble + "\\makeatother\n\n";
1948
1949         // We try to load babel late, in case it interferes with other packages.
1950         // Jurabib, hyperref, varioref and listings (bug 8995) have to be
1951         // called after babel, though.
1952         if (use_babel && !features.isRequired("jurabib")
1953             && !features.isRequired("hyperref")
1954                 && !features.isRequired("varioref")
1955             && !features.isRequired("vietnamese")
1956             && !features.isRequired("japanese")) {
1957                 // FIXME UNICODE
1958                 lyxpreamble += from_utf8(features.getBabelPresettings());
1959                 lyxpreamble += from_utf8(babelCall(language_options.str(),
1960                                                    features.needBabelLangOptions())) + '\n';
1961                 lyxpreamble += from_utf8(features.getBabelPostsettings());
1962         }
1963         if (!listings_params.empty() || features.isRequired("listings"))
1964                 lyxpreamble += "\\usepackage{listings}\n";
1965         if (!listings_params.empty()) {
1966                 lyxpreamble += "\\lstset{";
1967                 // do not test validity because listings_params is
1968                 // supposed to be valid
1969                 string par =
1970                         InsetListingsParams(listings_params).separatedParams(true);
1971                 lyxpreamble += from_utf8(par);
1972                 lyxpreamble += "}\n";
1973         }
1974
1975         // xunicode needs to be loaded at least after amsmath, amssymb,
1976         // esint and the other packages that provide special glyphs
1977         if (features.runparams().flavor == OutputParams::XETEX
1978             && useNonTeXFonts)
1979                 lyxpreamble += "\\usepackage{xunicode}\n";
1980
1981         // Polyglossia must be loaded last
1982         if (use_polyglossia) {
1983                 // call the package
1984                 lyxpreamble += "\\usepackage{polyglossia}\n";
1985                 // set the main language
1986                 lyxpreamble += "\\setdefaultlanguage";
1987                 if (!language->polyglossiaOpts().empty())
1988                         lyxpreamble += "[" + from_ascii(language->polyglossiaOpts()) + "]";
1989                 lyxpreamble += "{" + from_ascii(language->polyglossia()) + "}\n";
1990                 // now setup the other languages
1991                 std::map<std::string, std::string> const polylangs =
1992                         features.getPolyglossiaLanguages();
1993                 for (std::map<std::string, std::string>::const_iterator mit = polylangs.begin();
1994                      mit != polylangs.end() ; ++mit) {
1995                         lyxpreamble += "\\setotherlanguage";
1996                         if (!mit->second.empty())
1997                                 lyxpreamble += "[" + from_ascii(mit->second) + "]";
1998                         lyxpreamble += "{" + from_ascii(mit->first) + "}\n";
1999                 }
2000         }
2001
2002         // Load custom language package here
2003         if (features.langPackage() == LaTeXFeatures::LANG_PACK_CUSTOM) {
2004                 if (lang_package == "default")
2005                         lyxpreamble += from_utf8(lyxrc.language_custom_package);
2006                 else
2007                         lyxpreamble += from_utf8(lang_package);
2008                 lyxpreamble += '\n';
2009         }
2010
2011         docstring const i18npreamble =
2012                 features.getTClassI18nPreamble(use_babel, use_polyglossia);
2013         if (!i18npreamble.empty())
2014                 lyxpreamble += i18npreamble + '\n';
2015
2016         os << lyxpreamble;
2017
2018         return use_babel;
2019 }
2020
2021
2022 void BufferParams::useClassDefaults()
2023 {
2024         DocumentClass const & tclass = documentClass();
2025
2026         sides = tclass.sides();
2027         columns = tclass.columns();
2028         pagestyle = tclass.pagestyle();
2029         use_default_options = true;
2030         // Only if class has a ToC hierarchy
2031         if (tclass.hasTocLevels()) {
2032                 secnumdepth = tclass.secnumdepth();
2033                 tocdepth = tclass.tocdepth();
2034         }
2035 }
2036
2037
2038 bool BufferParams::hasClassDefaults() const
2039 {
2040         DocumentClass const & tclass = documentClass();
2041
2042         return sides == tclass.sides()
2043                 && columns == tclass.columns()
2044                 && pagestyle == tclass.pagestyle()
2045                 && use_default_options
2046                 && secnumdepth == tclass.secnumdepth()
2047                 && tocdepth == tclass.tocdepth();
2048 }
2049
2050
2051 DocumentClass const & BufferParams::documentClass() const
2052 {
2053         return *doc_class_.get();
2054 }
2055
2056
2057 DocumentClassConstPtr BufferParams::documentClassPtr() const
2058 {
2059         return doc_class_;
2060 }
2061
2062
2063 void BufferParams::setDocumentClass(DocumentClassConstPtr tc)
2064 {
2065         // evil, but this function is evil
2066         doc_class_ = const_pointer_cast<DocumentClass>(tc);
2067 }
2068
2069
2070 bool BufferParams::setBaseClass(string const & classname)
2071 {
2072         LYXERR(Debug::TCLASS, "setBaseClass: " << classname);
2073         LayoutFileList & bcl = LayoutFileList::get();
2074         if (!bcl.haveClass(classname)) {
2075                 docstring s =
2076                         bformat(_("The layout file:\n"
2077                                 "%1$s\n"
2078                                 "could not be found. A default textclass with default\n"
2079                                 "layouts will be used. LyX will not be able to produce\n"
2080                                 "correct output."),
2081                         from_utf8(classname));
2082                 frontend::Alert::error(_("Document class not found"), s);
2083                 bcl.addEmptyClass(classname);
2084         }
2085
2086         bool const success = bcl[classname].load();
2087         if (!success) {
2088                 docstring s =
2089                         bformat(_("Due to some error in it, the layout file:\n"
2090                                 "%1$s\n"
2091                                 "could not be loaded. A default textclass with default\n"
2092                                 "layouts will be used. LyX will not be able to produce\n"
2093                                 "correct output."),
2094                         from_utf8(classname));
2095                 frontend::Alert::error(_("Could not load class"), s);
2096                 bcl.addEmptyClass(classname);
2097         }
2098
2099         pimpl_->baseClass_ = classname;
2100         layout_modules_.adaptToBaseClass(baseClass(), removed_modules_);
2101         return true;
2102 }
2103
2104
2105 LayoutFile const * BufferParams::baseClass() const
2106 {
2107         if (LayoutFileList::get().haveClass(pimpl_->baseClass_))
2108                 return &(LayoutFileList::get()[pimpl_->baseClass_]);
2109         else
2110                 return 0;
2111 }
2112
2113
2114 LayoutFileIndex const & BufferParams::baseClassID() const
2115 {
2116         return pimpl_->baseClass_;
2117 }
2118
2119
2120 void BufferParams::makeDocumentClass(bool const clone)
2121 {
2122         if (!baseClass())
2123                 return;
2124
2125         LayoutModuleList mods;
2126         LayoutModuleList::iterator it = layout_modules_.begin();
2127         LayoutModuleList::iterator en = layout_modules_.end();
2128         for (; it != en; ++it)
2129                 mods.push_back(*it);
2130
2131         it = cite_engine_.begin();
2132         en = cite_engine_.end();
2133         for (; it != en; ++it)
2134                 mods.push_back(*it);
2135
2136         doc_class_ = getDocumentClass(*baseClass(), mods, clone);
2137
2138         TextClass::ReturnValues success = TextClass::OK;
2139         if (!forced_local_layout_.empty())
2140                 success = doc_class_->read(forced_local_layout_, TextClass::MODULE);
2141         if (!local_layout_.empty() &&
2142             (success == TextClass::OK || success == TextClass::OK_OLDFORMAT))
2143                 success = doc_class_->read(local_layout_, TextClass::MODULE);
2144         if (success != TextClass::OK && success != TextClass::OK_OLDFORMAT) {
2145                 docstring const msg = _("Error reading internal layout information");
2146                 frontend::Alert::warning(_("Read Error"), msg);
2147         }
2148 }
2149
2150
2151 bool BufferParams::layoutModuleCanBeAdded(string const & modName) const
2152 {
2153         return layout_modules_.moduleCanBeAdded(modName, baseClass());
2154 }
2155
2156
2157 bool BufferParams::citationModuleCanBeAdded(string const & modName) const
2158 {
2159         return cite_engine_.moduleCanBeAdded(modName, baseClass());
2160 }
2161
2162
2163 std::string BufferParams::getLocalLayout(bool forced) const
2164 {
2165         if (forced)
2166                 return doc_class_->forcedLayouts();
2167         else
2168                 return local_layout_;
2169 }
2170
2171
2172 void BufferParams::setLocalLayout(string const & layout, bool forced)
2173 {
2174         if (forced)
2175                 forced_local_layout_ = layout;
2176         else
2177                 local_layout_ = layout;
2178 }
2179
2180
2181 bool BufferParams::addLayoutModule(string const & modName)
2182 {
2183         LayoutModuleList::const_iterator it = layout_modules_.begin();
2184         LayoutModuleList::const_iterator end = layout_modules_.end();
2185         for (; it != end; ++it)
2186                 if (*it == modName)
2187                         return false;
2188         layout_modules_.push_back(modName);
2189         return true;
2190 }
2191
2192
2193 string BufferParams::bufferFormat() const
2194 {
2195         string format = documentClass().outputFormat();
2196         if (format == "latex") {
2197                 if (useNonTeXFonts)
2198                         return "xetex";
2199                 if (encoding().package() == Encoding::japanese)
2200                         return "platex";
2201         }
2202         return format;
2203 }
2204
2205
2206 bool BufferParams::isExportable(string const & format) const
2207 {
2208         vector<string> backs = backends();
2209         for (vector<string>::const_iterator it = backs.begin();
2210              it != backs.end(); ++it)
2211                 if (theConverters().isReachable(*it, format))
2212                         return true;
2213         return false;
2214 }
2215
2216
2217 vector<Format const *> BufferParams::exportableFormats(bool only_viewable) const
2218 {
2219         vector<string> const backs = backends();
2220         set<string> excludes;
2221         if (useNonTeXFonts) {
2222                 excludes.insert("latex");
2223                 excludes.insert("pdflatex");
2224         }
2225         vector<Format const *> result =
2226                 theConverters().getReachable(backs[0], only_viewable, true, excludes);
2227         for (vector<string>::const_iterator it = backs.begin() + 1;
2228              it != backs.end(); ++it) {
2229                 vector<Format const *>  r =
2230                         theConverters().getReachable(*it, only_viewable, false, excludes);
2231                 result.insert(result.end(), r.begin(), r.end());
2232         }
2233         return result;
2234 }
2235
2236
2237 bool BufferParams::isExportableFormat(string const & format) const
2238 {
2239         typedef vector<Format const *> Formats;
2240         Formats formats;
2241         formats = exportableFormats(true);
2242         Formats::const_iterator fit = formats.begin();
2243         Formats::const_iterator end = formats.end();
2244         for (; fit != end ; ++fit) {
2245                 if ((*fit)->name() == format)
2246                         return true;
2247         }
2248         return false;
2249 }
2250
2251
2252 vector<string> BufferParams::backends() const
2253 {
2254         vector<string> v;
2255         string const buffmt = bufferFormat();
2256
2257         // FIXME: Don't hardcode format names here, but use a flag
2258         if (buffmt == "latex") {
2259                 if (!useNonTeXFonts) {
2260                         v.push_back("pdflatex");
2261                         v.push_back("latex");
2262                 }
2263                 v.push_back("luatex");
2264                 v.push_back("dviluatex");
2265                 v.push_back("xetex");
2266         } else if (buffmt == "xetex") {
2267                 v.push_back("xetex");
2268                 // FIXME: need to test all languages (bug 8205)
2269                 if (!language || !language->isPolyglossiaExclusive()) {
2270                         v.push_back("luatex");
2271                         v.push_back("dviluatex");
2272                 }
2273         } else
2274                 v.push_back(buffmt);
2275
2276         v.push_back("xhtml");
2277         v.push_back("text");
2278         v.push_back("lyx");
2279         return v;
2280 }
2281
2282
2283 OutputParams::FLAVOR BufferParams::getOutputFlavor(string const & format) const
2284 {
2285         string const dformat = (format.empty() || format == "default") ?
2286                 getDefaultOutputFormat() : format;
2287         DefaultFlavorCache::const_iterator it =
2288                 default_flavors_.find(dformat);
2289
2290         if (it != default_flavors_.end())
2291                 return it->second;
2292
2293         OutputParams::FLAVOR result = OutputParams::LATEX;
2294
2295         // FIXME It'd be better not to hardcode this, but to do
2296         //       something with formats.
2297         if (dformat == "xhtml")
2298                 result = OutputParams::HTML;
2299         else if (dformat == "text")
2300                 result = OutputParams::TEXT;
2301         else if (dformat == "lyx")
2302                 result = OutputParams::LYX;
2303         else if (dformat == "pdflatex")
2304                 result = OutputParams::PDFLATEX;
2305         else if (dformat == "xetex")
2306                 result = OutputParams::XETEX;
2307         else if (dformat == "luatex")
2308                 result = OutputParams::LUATEX;
2309         else if (dformat == "dviluatex")
2310                 result = OutputParams::DVILUATEX;
2311         else {
2312                 // Try to determine flavor of default output format
2313                 vector<string> backs = backends();
2314                 if (find(backs.begin(), backs.end(), dformat) == backs.end()) {
2315                         // Get shortest path to format
2316                         Graph::EdgePath path;
2317                         for (vector<string>::const_iterator it = backs.begin();
2318                             it != backs.end(); ++it) {
2319                                 Graph::EdgePath p = theConverters().getPath(*it, dformat);
2320                                 if (!p.empty() && (path.empty() || p.size() < path.size())) {
2321                                         path = p;
2322                                 }
2323                         }
2324                         if (!path.empty())
2325                                 result = theConverters().getFlavor(path);
2326                 }
2327         }
2328         // cache this flavor
2329         default_flavors_[dformat] = result;
2330         return result;
2331 }
2332
2333
2334 string BufferParams::getDefaultOutputFormat() const
2335 {
2336         if (!default_output_format.empty()
2337             && default_output_format != "default")
2338                 return default_output_format;
2339         if (isDocBook()
2340             || encoding().package() == Encoding::japanese) {
2341                 vector<Format const *> const formats = exportableFormats(true);
2342                 if (formats.empty())
2343                         return string();
2344                 // return the first we find
2345                 return formats.front()->name();
2346         }
2347         if (useNonTeXFonts)
2348                 return lyxrc.default_otf_view_format;
2349         return lyxrc.default_view_format;
2350 }
2351
2352 Font const BufferParams::getFont() const
2353 {
2354         FontInfo f = documentClass().defaultfont();
2355         if (fonts_default_family == "rmdefault")
2356                 f.setFamily(ROMAN_FAMILY);
2357         else if (fonts_default_family == "sfdefault")
2358                 f.setFamily(SANS_FAMILY);
2359         else if (fonts_default_family == "ttdefault")
2360                 f.setFamily(TYPEWRITER_FAMILY);
2361         return Font(f, language);
2362 }
2363
2364
2365 InsetQuotes::QuoteLanguage BufferParams::getQuoteStyle(string const & qs) const
2366 {
2367         return quoteslangtranslator().find(qs);
2368 }
2369
2370
2371 bool BufferParams::isLatex() const
2372 {
2373         return documentClass().outputType() == LATEX;
2374 }
2375
2376
2377 bool BufferParams::isLiterate() const
2378 {
2379         return documentClass().outputType() == LITERATE;
2380 }
2381
2382
2383 bool BufferParams::isDocBook() const
2384 {
2385         return documentClass().outputType() == DOCBOOK;
2386 }
2387
2388
2389 void BufferParams::readPreamble(Lexer & lex)
2390 {
2391         if (lex.getString() != "\\begin_preamble")
2392                 lyxerr << "Error (BufferParams::readPreamble):"
2393                         "consistency check failed." << endl;
2394
2395         preamble = lex.getLongString("\\end_preamble");
2396 }
2397
2398
2399 void BufferParams::readLocalLayout(Lexer & lex, bool forced)
2400 {
2401         string const expected = forced ? "\\begin_forced_local_layout" :
2402                                          "\\begin_local_layout";
2403         if (lex.getString() != expected)
2404                 lyxerr << "Error (BufferParams::readLocalLayout):"
2405                         "consistency check failed." << endl;
2406
2407         if (forced)
2408                 forced_local_layout_ =
2409                         lex.getLongString("\\end_forced_local_layout");
2410         else
2411                 local_layout_ = lex.getLongString("\\end_local_layout");
2412 }
2413
2414
2415 bool BufferParams::setLanguage(string const & lang)
2416 {
2417         Language const *new_language = languages.getLanguage(lang);
2418         if (!new_language) {
2419                 // Language lang was not found
2420                 return false;
2421         }
2422         language = new_language;
2423         return true;
2424 }
2425
2426
2427 void BufferParams::readLanguage(Lexer & lex)
2428 {
2429         if (!lex.next()) return;
2430
2431         string const tmptok = lex.getString();
2432
2433         // check if tmptok is part of tex_babel in tex-defs.h
2434         if (!setLanguage(tmptok)) {
2435                 // Language tmptok was not found
2436                 language = default_language;
2437                 lyxerr << "Warning: Setting language `"
2438                        << tmptok << "' to `" << language->lang()
2439                        << "'." << endl;
2440         }
2441 }
2442
2443
2444 void BufferParams::readGraphicsDriver(Lexer & lex)
2445 {
2446         if (!lex.next())
2447                 return;
2448
2449         string const tmptok = lex.getString();
2450         // check if tmptok is part of tex_graphics in tex_defs.h
2451         int n = 0;
2452         while (true) {
2453                 string const test = tex_graphics[n++];
2454
2455                 if (test == tmptok) {
2456                         graphics_driver = tmptok;
2457                         break;
2458                 }
2459                 if (test.empty()) {
2460                         lex.printError(
2461                                 "Warning: graphics driver `$$Token' not recognized!\n"
2462                                 "         Setting graphics driver to `default'.\n");
2463                         graphics_driver = "default";
2464                         break;
2465                 }
2466         }
2467 }
2468
2469
2470 void BufferParams::readBullets(Lexer & lex)
2471 {
2472         if (!lex.next())
2473                 return;
2474
2475         int const index = lex.getInteger();
2476         lex.next();
2477         int temp_int = lex.getInteger();
2478         user_defined_bullet(index).setFont(temp_int);
2479         temp_bullet(index).setFont(temp_int);
2480         lex >> temp_int;
2481         user_defined_bullet(index).setCharacter(temp_int);
2482         temp_bullet(index).setCharacter(temp_int);
2483         lex >> temp_int;
2484         user_defined_bullet(index).setSize(temp_int);
2485         temp_bullet(index).setSize(temp_int);
2486 }
2487
2488
2489 void BufferParams::readBulletsLaTeX(Lexer & lex)
2490 {
2491         // The bullet class should be able to read this.
2492         if (!lex.next())
2493                 return;
2494         int const index = lex.getInteger();
2495         lex.next(true);
2496         docstring const temp_str = lex.getDocString();
2497
2498         user_defined_bullet(index).setText(temp_str);
2499         temp_bullet(index).setText(temp_str);
2500 }
2501
2502
2503 void BufferParams::readModules(Lexer & lex)
2504 {
2505         if (!lex.eatLine()) {
2506                 lyxerr << "Error (BufferParams::readModules):"
2507                                 "Unexpected end of input." << endl;
2508                 return;
2509         }
2510         while (true) {
2511                 string mod = lex.getString();
2512                 if (mod == "\\end_modules")
2513                         break;
2514                 addLayoutModule(mod);
2515                 lex.eatLine();
2516         }
2517 }
2518
2519
2520 void BufferParams::readRemovedModules(Lexer & lex)
2521 {
2522         if (!lex.eatLine()) {
2523                 lyxerr << "Error (BufferParams::readRemovedModules):"
2524                                 "Unexpected end of input." << endl;
2525                 return;
2526         }
2527         while (true) {
2528                 string mod = lex.getString();
2529                 if (mod == "\\end_removed_modules")
2530                         break;
2531                 removed_modules_.push_back(mod);
2532                 lex.eatLine();
2533         }
2534         // now we want to remove any removed modules that were previously
2535         // added. normally, that will be because default modules were added in
2536         // setBaseClass(), which gets called when \textclass is read at the
2537         // start of the read.
2538         list<string>::const_iterator rit = removed_modules_.begin();
2539         list<string>::const_iterator const ren = removed_modules_.end();
2540         for (; rit != ren; ++rit) {
2541                 LayoutModuleList::iterator const mit = layout_modules_.begin();
2542                 LayoutModuleList::iterator const men = layout_modules_.end();
2543                 LayoutModuleList::iterator found = find(mit, men, *rit);
2544                 if (found == men)
2545                         continue;
2546                 layout_modules_.erase(found);
2547         }
2548 }
2549
2550
2551 void BufferParams::readIncludeonly(Lexer & lex)
2552 {
2553         if (!lex.eatLine()) {
2554                 lyxerr << "Error (BufferParams::readIncludeonly):"
2555                                 "Unexpected end of input." << endl;
2556                 return;
2557         }
2558         while (true) {
2559                 string child = lex.getString();
2560                 if (child == "\\end_includeonly")
2561                         break;
2562                 included_children_.push_back(child);
2563                 lex.eatLine();
2564         }
2565 }
2566
2567
2568 string BufferParams::paperSizeName(PapersizePurpose purpose) const
2569 {
2570         switch (papersize) {
2571         case PAPER_DEFAULT:
2572                 // could be anything, so don't guess
2573                 return string();
2574         case PAPER_CUSTOM: {
2575                 if (purpose == XDVI && !paperwidth.empty() &&
2576                     !paperheight.empty()) {
2577                         // heightxwidth<unit>
2578                         string first = paperwidth;
2579                         string second = paperheight;
2580                         if (orientation == ORIENTATION_LANDSCAPE)
2581                                 first.swap(second);
2582                         // cut off unit.
2583                         return first.erase(first.length() - 2)
2584                                 + "x" + second;
2585                 }
2586                 return string();
2587         }
2588         case PAPER_A0:
2589                 // dvips and dvipdfm do not know this
2590                 if (purpose == DVIPS || purpose == DVIPDFM)
2591                         return string();
2592                 return "a0";
2593         case PAPER_A1:
2594                 if (purpose == DVIPS || purpose == DVIPDFM)
2595                         return string();
2596                 return "a1";
2597         case PAPER_A2:
2598                 if (purpose == DVIPS || purpose == DVIPDFM)
2599                         return string();
2600                 return "a2";
2601         case PAPER_A3:
2602                 return "a3";
2603         case PAPER_A4:
2604                 return "a4";
2605         case PAPER_A5:
2606                 return "a5";
2607         case PAPER_A6:
2608                 if (purpose == DVIPS || purpose == DVIPDFM)
2609                         return string();
2610                 return "a6";
2611         case PAPER_B0:
2612                 if (purpose == DVIPS || purpose == DVIPDFM)
2613                         return string();
2614                 return "b0";
2615         case PAPER_B1:
2616                 if (purpose == DVIPS || purpose == DVIPDFM)
2617                         return string();
2618                 return "b1";
2619         case PAPER_B2:
2620                 if (purpose == DVIPS || purpose == DVIPDFM)
2621                         return string();
2622                 return "b2";
2623         case PAPER_B3:
2624                 if (purpose == DVIPS || purpose == DVIPDFM)
2625                         return string();
2626                 return "b3";
2627         case PAPER_B4:
2628                 // dvipdfm does not know this
2629                 if (purpose == DVIPDFM)
2630                         return string();
2631                 return "b4";
2632         case PAPER_B5:
2633                 if (purpose == DVIPDFM)
2634                         return string();
2635                 return "b5";
2636         case PAPER_B6:
2637                 if (purpose == DVIPS || purpose == DVIPDFM)
2638                         return string();
2639                 return "b6";
2640         case PAPER_C0:
2641                 if (purpose == DVIPS || purpose == DVIPDFM)
2642                         return string();
2643                 return "c0";
2644         case PAPER_C1:
2645                 if (purpose == DVIPS || purpose == DVIPDFM)
2646                         return string();
2647                 return "c1";
2648         case PAPER_C2:
2649                 if (purpose == DVIPS || purpose == DVIPDFM)
2650                         return string();
2651                 return "c2";
2652         case PAPER_C3:
2653                 if (purpose == DVIPS || purpose == DVIPDFM)
2654                         return string();
2655                 return "c3";
2656         case PAPER_C4:
2657                 if (purpose == DVIPS || purpose == DVIPDFM)
2658                         return string();
2659                 return "c4";
2660         case PAPER_C5:
2661                 if (purpose == DVIPS || purpose == DVIPDFM)
2662                         return string();
2663                 return "c5";
2664         case PAPER_C6:
2665                 if (purpose == DVIPS || purpose == DVIPDFM)
2666                         return string();
2667                 return "c6";
2668         case PAPER_JISB0:
2669                 if (purpose == DVIPS || purpose == DVIPDFM)
2670                         return string();
2671                 return "jisb0";
2672         case PAPER_JISB1:
2673                 if (purpose == DVIPS || purpose == DVIPDFM)
2674                         return string();
2675                 return "jisb1";
2676         case PAPER_JISB2:
2677                 if (purpose == DVIPS || purpose == DVIPDFM)
2678                         return string();
2679                 return "jisb2";
2680         case PAPER_JISB3:
2681                 if (purpose == DVIPS || purpose == DVIPDFM)
2682                         return string();
2683                 return "jisb3";
2684         case PAPER_JISB4:
2685                 if (purpose == DVIPS || purpose == DVIPDFM)
2686                         return string();
2687                 return "jisb4";
2688         case PAPER_JISB5:
2689                 if (purpose == DVIPS || purpose == DVIPDFM)
2690                         return string();
2691                 return "jisb5";
2692         case PAPER_JISB6:
2693                 if (purpose == DVIPS || purpose == DVIPDFM)
2694                         return string();
2695                 return "jisb6";
2696         case PAPER_USEXECUTIVE:
2697                 // dvipdfm does not know this
2698                 if (purpose == DVIPDFM)
2699                         return string();
2700                 return "foolscap";
2701         case PAPER_USLEGAL:
2702                 return "legal";
2703         case PAPER_USLETTER:
2704         default:
2705                 if (purpose == XDVI)
2706                         return "us";
2707                 return "letter";
2708         }
2709 }
2710
2711
2712 string const BufferParams::dvips_options() const
2713 {
2714         string result;
2715
2716         // If the class loads the geometry package, we do not know which
2717         // paper size is used, since we do not set it (bug 7013).
2718         // Therefore we must not specify any argument here.
2719         // dvips gets the correct paper size via DVI specials in this case
2720         // (if the class uses the geometry package correctly).
2721         if (documentClass().provides("geometry"))
2722                 return result;
2723
2724         if (use_geometry
2725             && papersize == PAPER_CUSTOM
2726             && !lyxrc.print_paper_dimension_flag.empty()
2727             && !paperwidth.empty()
2728             && !paperheight.empty()) {
2729                 // using a custom papersize
2730                 result = lyxrc.print_paper_dimension_flag;
2731                 result += ' ' + paperwidth;
2732                 result += ',' + paperheight;
2733         } else {
2734                 string const paper_option = paperSizeName(DVIPS);
2735                 if (!paper_option.empty() && (paper_option != "letter" ||
2736                     orientation != ORIENTATION_LANDSCAPE)) {
2737                         // dvips won't accept -t letter -t landscape.
2738                         // In all other cases, include the paper size
2739                         // explicitly.
2740                         result = lyxrc.print_paper_flag;
2741                         result += ' ' + paper_option;
2742                 }
2743         }
2744         if (orientation == ORIENTATION_LANDSCAPE &&
2745             papersize != PAPER_CUSTOM)
2746                 result += ' ' + lyxrc.print_landscape_flag;
2747         return result;
2748 }
2749
2750
2751 string const BufferParams::font_encoding() const
2752 {
2753         return (fontenc == "global") ? lyxrc.fontenc : fontenc;
2754 }
2755
2756
2757 string BufferParams::babelCall(string const & lang_opts, bool const langoptions) const
2758 {
2759         // suppress the babel call if there is no BabelName defined
2760         // for the document language in the lib/languages file and if no
2761         // other languages are used (lang_opts is then empty)
2762         if (lang_opts.empty())
2763                 return string();
2764         // either a specific language (AsBabelOptions setting in
2765         // lib/languages) or the prefs require the languages to
2766         // be submitted to babel itself (not the class).
2767         if (langoptions)
2768                 return "\\usepackage[" + lang_opts + "]{babel}";
2769         return "\\usepackage{babel}";
2770 }
2771
2772
2773 docstring BufferParams::getGraphicsDriver(string const & package) const
2774 {
2775         docstring result;
2776
2777         if (package == "geometry") {
2778                 if (graphics_driver == "dvips"
2779                     || graphics_driver == "dvipdfm"
2780                     || graphics_driver == "pdftex"
2781                     || graphics_driver == "vtex")
2782                         result = from_ascii(graphics_driver);
2783                 else if (graphics_driver == "dvipdfmx")
2784                         result = from_ascii("dvipdfm");
2785         }
2786
2787         return result;
2788 }
2789
2790
2791 void BufferParams::writeEncodingPreamble(otexstream & os,
2792                                          LaTeXFeatures & features) const
2793 {
2794         // XeTeX does not need this
2795         if (features.runparams().flavor == OutputParams::XETEX)
2796                 return;
2797         // LuaTeX neither, but with tex fonts, we need to load
2798         // the luainputenc package.
2799         if (features.runparams().flavor == OutputParams::LUATEX
2800                 || features.runparams().flavor == OutputParams::DVILUATEX) {
2801                 if (!useNonTeXFonts && inputenc != "default"
2802                     && ((inputenc == "auto" && language->encoding()->package() == Encoding::inputenc)
2803                         || (inputenc != "auto" && encoding().package() == Encoding::inputenc))) {
2804                         os << "\\usepackage[utf8]{luainputenc}\n";
2805                 }
2806                 return;
2807         }
2808         if (inputenc == "auto") {
2809                 string const doc_encoding =
2810                         language->encoding()->latexName();
2811                 Encoding::Package const package =
2812                         language->encoding()->package();
2813
2814                 // Create a list with all the input encodings used
2815                 // in the document
2816                 set<string> encodings =
2817                         features.getEncodingSet(doc_encoding);
2818
2819                 // If the "japanese" package (i.e. pLaTeX) is used,
2820                 // inputenc must be omitted.
2821                 // see http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg129680.html
2822                 if ((!encodings.empty() || package == Encoding::inputenc)
2823                     && !features.isRequired("japanese")) {
2824                         os << "\\usepackage[";
2825                         set<string>::const_iterator it = encodings.begin();
2826                         set<string>::const_iterator const end = encodings.end();
2827                         if (it != end) {
2828                                 os << from_ascii(*it);
2829                                 ++it;
2830                         }
2831                         for (; it != end; ++it)
2832                                 os << ',' << from_ascii(*it);
2833                         if (package == Encoding::inputenc) {
2834                                 if (!encodings.empty())
2835                                         os << ',';
2836                                 os << from_ascii(doc_encoding);
2837                         }
2838                         os << "]{inputenc}\n";
2839                 }
2840                 if (package == Encoding::CJK || features.mustProvide("CJK")) {
2841                         if (language->encoding()->name() == "utf8-cjk"
2842                             && LaTeXFeatures::isAvailable("CJKutf8"))
2843                                 os << "\\usepackage{CJKutf8}\n";
2844                         else
2845                                 os << "\\usepackage{CJK}\n";
2846                 }
2847         } else if (inputenc != "default") {
2848                 switch (encoding().package()) {
2849                 case Encoding::none:
2850                 case Encoding::japanese:
2851                         break;
2852                 case Encoding::inputenc:
2853                         // do not load inputenc if japanese is used
2854                         if (features.isRequired("japanese"))
2855                                 break;
2856                         os << "\\usepackage[" << from_ascii(encoding().latexName())
2857                            << "]{inputenc}\n";
2858                         break;
2859                 case Encoding::CJK:
2860                         if (encoding().name() == "utf8-cjk"
2861                             && LaTeXFeatures::isAvailable("CJKutf8"))
2862                                 os << "\\usepackage{CJKutf8}\n";
2863                         else
2864                                 os << "\\usepackage{CJK}\n";
2865                         break;
2866                 }
2867                 // Load the CJK package if needed by a secondary language.
2868                 // If the main encoding is some variant of UTF8, use CJKutf8.
2869                 if (encoding().package() != Encoding::CJK && features.mustProvide("CJK")) {
2870                         if (encoding().iconvName() == "UTF-8"
2871                             && LaTeXFeatures::isAvailable("CJKutf8"))
2872                                 os << "\\usepackage{CJKutf8}\n";
2873                         else
2874                                 os << "\\usepackage{CJK}\n";
2875                 }
2876         }
2877 }
2878
2879
2880 string const BufferParams::parseFontName(string const & name) const
2881 {
2882         string mangled = name;
2883         size_t const idx = mangled.find('[');
2884         if (idx == string::npos || idx == 0)
2885                 return mangled;
2886         else
2887                 return mangled.substr(0, idx - 1);
2888 }
2889
2890
2891 string const BufferParams::loadFonts(LaTeXFeatures & features) const
2892 {
2893         if (fonts_roman == "default" && fonts_sans == "default"
2894             && fonts_typewriter == "default"
2895             && (fonts_math == "default" || fonts_math == "auto"))
2896                 //nothing to do
2897                 return string();
2898
2899         ostringstream os;
2900
2901         /* Fontspec (XeTeX, LuaTeX): we provide GUI support for oldstyle
2902          * numbers (Numbers=OldStyle) and sf/tt scaling. The Ligatures=TeX/
2903          * Mapping=tex-text option assures TeX ligatures (such as "--")
2904          * are resolved. Note that tt does not use these ligatures.
2905          * TODO:
2906          *    -- add more GUI options?
2907          *    -- add more fonts (fonts for other scripts)
2908          *    -- if there's a way to find out if a font really supports
2909          *       OldStyle, enable/disable the widget accordingly.
2910         */
2911         if (useNonTeXFonts && features.isAvailable("fontspec")) {
2912                 // "Mapping=tex-text" and "Ligatures=TeX" are equivalent.
2913                 // However, until v.2 (2010/07/11) fontspec only knew
2914                 // Mapping=tex-text (for XeTeX only); then "Ligatures=TeX"
2915                 // was introduced for both XeTeX and LuaTeX (LuaTeX
2916                 // didn't understand "Mapping=tex-text", while XeTeX
2917                 // understood both. With most recent versions, both
2918                 // variants are understood by both engines. However,
2919                 // we want to provide support for at least TeXLive 2009
2920                 // (for XeTeX; LuaTeX is only supported as of v.2)
2921                 string const texmapping =
2922                         (features.runparams().flavor == OutputParams::XETEX) ?
2923                         "Mapping=tex-text" : "Ligatures=TeX";
2924                 if (fonts_roman != "default") {
2925                         os << "\\setmainfont[" << texmapping;
2926                         if (fonts_old_figures)
2927                                 os << ",Numbers=OldStyle";
2928                         os << "]{" << parseFontName(fonts_roman) << "}\n";
2929                 }
2930                 if (fonts_sans != "default") {
2931                         string const sans = parseFontName(fonts_sans);
2932                         if (fonts_sans_scale != 100)
2933                                 os << "\\setsansfont[Scale="
2934                                    << float(fonts_sans_scale) / 100
2935                                    << "," << texmapping << "]{"
2936                                    << sans << "}\n";
2937                         else
2938                                 os << "\\setsansfont[" << texmapping << "]{"
2939                                    << sans << "}\n";
2940                 }
2941                 if (fonts_typewriter != "default") {
2942                         string const mono = parseFontName(fonts_typewriter);
2943                         if (fonts_typewriter_scale != 100)
2944                                 os << "\\setmonofont[Scale="
2945                                    << float(fonts_typewriter_scale) / 100
2946                                    << "]{"
2947                                    << mono << "}\n";
2948                         else
2949                                 os << "\\setmonofont{"
2950                                    << mono << "}\n";
2951                 }
2952                 return os.str();
2953         }
2954
2955         // Tex Fonts
2956         bool const ot1 = (font_encoding() == "default" || font_encoding() == "OT1");
2957         bool const dryrun = features.runparams().dryrun;
2958         bool const complete = (fonts_sans == "default" && fonts_typewriter == "default");
2959         bool const nomath = (fonts_math == "default");
2960
2961         // ROMAN FONTS
2962         os << theLaTeXFonts().getLaTeXFont(from_ascii(fonts_roman)).getLaTeXCode(
2963                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
2964                 nomath);
2965
2966         // SANS SERIF
2967         os << theLaTeXFonts().getLaTeXFont(from_ascii(fonts_sans)).getLaTeXCode(
2968                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
2969                 nomath, fonts_sans_scale);
2970
2971         // MONOSPACED/TYPEWRITER
2972         os << theLaTeXFonts().getLaTeXFont(from_ascii(fonts_typewriter)).getLaTeXCode(
2973                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
2974                 nomath, fonts_typewriter_scale);
2975
2976         // MATH
2977         os << theLaTeXFonts().getLaTeXFont(from_ascii(fonts_math)).getLaTeXCode(
2978                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
2979                 nomath);
2980
2981         return os.str();
2982 }
2983
2984
2985 Encoding const & BufferParams::encoding() const
2986 {
2987         // FIXME: actually, we should check for the flavor
2988         // or runparams.isFullyUnicode() here:
2989         // This check will not work with XeTeX/LuaTeX and tex fonts.
2990         // Thus we have to reset the encoding in Buffer::makeLaTeXFile.
2991         if (useNonTeXFonts)
2992                 return *(encodings.fromLyXName("utf8-plain"));
2993         if (inputenc == "auto" || inputenc == "default")
2994                 return *language->encoding();
2995         Encoding const * const enc = encodings.fromLyXName(inputenc);
2996         if (enc)
2997                 return *enc;
2998         LYXERR0("Unknown inputenc value `" << inputenc
2999                << "'. Using `auto' instead.");
3000         return *language->encoding();
3001 }
3002
3003
3004 bool BufferParams::addCiteEngine(string const & engine)
3005 {
3006         LayoutModuleList::const_iterator it = cite_engine_.begin();
3007         LayoutModuleList::const_iterator en = cite_engine_.end();
3008         for (; it != en; ++it)
3009                 if (*it == engine)
3010                         return false;
3011         cite_engine_.push_back(engine);
3012         return true;
3013 }
3014
3015
3016 bool BufferParams::addCiteEngine(vector<string> const & engine)
3017 {
3018         vector<string>::const_iterator it = engine.begin();
3019         vector<string>::const_iterator en = engine.end();
3020         bool ret = true;
3021         for (; it != en; ++it)
3022                 if (!addCiteEngine(*it))
3023                         ret = false;
3024         return ret;
3025 }
3026
3027
3028 string const & BufferParams::defaultBiblioStyle() const
3029 {
3030         return documentClass().defaultBiblioStyle();
3031 }
3032
3033
3034 bool const & BufferParams::fullAuthorList() const
3035 {
3036         return documentClass().fullAuthorList();
3037 }
3038
3039
3040 void BufferParams::setCiteEngine(string const & engine)
3041 {
3042         clearCiteEngine();
3043         addCiteEngine(engine);
3044 }
3045
3046
3047 void BufferParams::setCiteEngine(vector<string> const & engine)
3048 {
3049         clearCiteEngine();
3050         addCiteEngine(engine);
3051 }
3052
3053
3054 vector<string> BufferParams::citeCommands() const
3055 {
3056         static CitationStyle const default_style;
3057         vector<string> commands =
3058                 documentClass().citeCommands(citeEngineType());
3059         if (commands.empty())
3060                 commands.push_back(default_style.cmd);
3061         return commands;
3062 }
3063
3064
3065 vector<CitationStyle> BufferParams::citeStyles() const
3066 {
3067         static CitationStyle const default_style;
3068         vector<CitationStyle> styles =
3069                 documentClass().citeStyles(citeEngineType());
3070         if (styles.empty())
3071                 styles.push_back(default_style);
3072         return styles;
3073 }
3074
3075 } // namespace lyx