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