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