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