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