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