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