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