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