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