]> git.lyx.org Git - lyx.git/blob - src/BufferParams.cpp
InsetMath: match the screen display with the EnsureMath behaviour in output
[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 OS 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                 set<string> const polylangs =
2186                         features.getPolyglossiaLanguages();
2187                 for (set<string>::const_iterator mit = polylangs.begin();
2188                      mit != polylangs.end() ; ++mit) {
2189                         // We do not output the options here; they are output in
2190                         // the language switch commands. This is safer if multiple
2191                         // varieties are used.
2192                         if (*mit == language->polyglossia())
2193                                 continue;
2194                         os << "\\setotherlanguage";
2195                         os << "{" << from_ascii(*mit) << "}\n";
2196                 }
2197         }
2198
2199         // Load custom language package here
2200         if (features.langPackage() == LaTeXFeatures::LANG_PACK_CUSTOM) {
2201                 if (lang_package == "default")
2202                         os << from_utf8(lyxrc.language_custom_package);
2203                 else
2204                         os << from_utf8(lang_package);
2205                 os << '\n';
2206         }
2207
2208         docstring const i18npreamble =
2209                 features.getTClassI18nPreamble(use_babel, use_polyglossia);
2210         if (!i18npreamble.empty())
2211                 os << i18npreamble + '\n';
2212
2213         return use_babel;
2214 }
2215
2216
2217 void BufferParams::useClassDefaults()
2218 {
2219         DocumentClass const & tclass = documentClass();
2220
2221         sides = tclass.sides();
2222         columns = tclass.columns();
2223         pagestyle = tclass.pagestyle();
2224         use_default_options = true;
2225         // Only if class has a ToC hierarchy
2226         if (tclass.hasTocLevels()) {
2227                 secnumdepth = tclass.secnumdepth();
2228                 tocdepth = tclass.tocdepth();
2229         }
2230 }
2231
2232
2233 bool BufferParams::hasClassDefaults() const
2234 {
2235         DocumentClass const & tclass = documentClass();
2236
2237         return sides == tclass.sides()
2238                 && columns == tclass.columns()
2239                 && pagestyle == tclass.pagestyle()
2240                 && use_default_options
2241                 && secnumdepth == tclass.secnumdepth()
2242                 && tocdepth == tclass.tocdepth();
2243 }
2244
2245
2246 DocumentClass const & BufferParams::documentClass() const
2247 {
2248         return *doc_class_;
2249 }
2250
2251
2252 DocumentClassConstPtr BufferParams::documentClassPtr() const
2253 {
2254         return doc_class_;
2255 }
2256
2257
2258 void BufferParams::setDocumentClass(DocumentClassConstPtr tc)
2259 {
2260         // evil, but this function is evil
2261         doc_class_ = const_pointer_cast<DocumentClass>(tc);
2262         invalidateConverterCache();
2263 }
2264
2265
2266 bool BufferParams::setBaseClass(string const & classname)
2267 {
2268         LYXERR(Debug::TCLASS, "setBaseClass: " << classname);
2269         LayoutFileList & bcl = LayoutFileList::get();
2270         if (!bcl.haveClass(classname)) {
2271                 docstring s =
2272                         bformat(_("The layout file:\n"
2273                                 "%1$s\n"
2274                                 "could not be found. A default textclass with default\n"
2275                                 "layouts will be used. LyX will not be able to produce\n"
2276                                 "correct output."),
2277                         from_utf8(classname));
2278                 frontend::Alert::error(_("Document class not found"), s);
2279                 bcl.addEmptyClass(classname);
2280         }
2281
2282         bool const success = bcl[classname].load();
2283         if (!success) {
2284                 docstring s =
2285                         bformat(_("Due to some error in it, the layout file:\n"
2286                                 "%1$s\n"
2287                                 "could not be loaded. A default textclass with default\n"
2288                                 "layouts will be used. LyX will not be able to produce\n"
2289                                 "correct output."),
2290                         from_utf8(classname));
2291                 frontend::Alert::error(_("Could not load class"), s);
2292                 bcl.addEmptyClass(classname);
2293         }
2294
2295         pimpl_->baseClass_ = classname;
2296         layout_modules_.adaptToBaseClass(baseClass(), removed_modules_);
2297         return true;
2298 }
2299
2300
2301 LayoutFile const * BufferParams::baseClass() const
2302 {
2303         if (LayoutFileList::get().haveClass(pimpl_->baseClass_))
2304                 return &(LayoutFileList::get()[pimpl_->baseClass_]);
2305         else
2306                 return 0;
2307 }
2308
2309
2310 LayoutFileIndex const & BufferParams::baseClassID() const
2311 {
2312         return pimpl_->baseClass_;
2313 }
2314
2315
2316 void BufferParams::makeDocumentClass(bool const clone)
2317 {
2318         if (!baseClass())
2319                 return;
2320
2321         invalidateConverterCache();
2322         LayoutModuleList mods;
2323         LayoutModuleList::iterator it = layout_modules_.begin();
2324         LayoutModuleList::iterator en = layout_modules_.end();
2325         for (; it != en; ++it)
2326                 mods.push_back(*it);
2327
2328         it = cite_engine_.begin();
2329         en = cite_engine_.end();
2330         for (; it != en; ++it)
2331                 mods.push_back(*it);
2332
2333         doc_class_ = getDocumentClass(*baseClass(), mods, clone);
2334
2335         TextClass::ReturnValues success = TextClass::OK;
2336         if (!forced_local_layout_.empty())
2337                 success = doc_class_->read(to_utf8(forced_local_layout_),
2338                                            TextClass::MODULE);
2339         if (!local_layout_.empty() &&
2340             (success == TextClass::OK || success == TextClass::OK_OLDFORMAT))
2341                 success = doc_class_->read(to_utf8(local_layout_), TextClass::MODULE);
2342         if (success != TextClass::OK && success != TextClass::OK_OLDFORMAT) {
2343                 docstring const msg = _("Error reading internal layout information");
2344                 frontend::Alert::warning(_("Read Error"), msg);
2345         }
2346 }
2347
2348
2349 bool BufferParams::layoutModuleCanBeAdded(string const & modName) const
2350 {
2351         return layout_modules_.moduleCanBeAdded(modName, baseClass());
2352 }
2353
2354
2355 bool BufferParams::citationModuleCanBeAdded(string const & modName) const
2356 {
2357         return cite_engine_.moduleCanBeAdded(modName, baseClass());
2358 }
2359
2360
2361 docstring BufferParams::getLocalLayout(bool forced) const
2362 {
2363         if (forced)
2364                 return from_utf8(doc_class_->forcedLayouts());
2365         else
2366                 return local_layout_;
2367 }
2368
2369
2370 void BufferParams::setLocalLayout(docstring const & layout, bool forced)
2371 {
2372         if (forced)
2373                 forced_local_layout_ = layout;
2374         else
2375                 local_layout_ = layout;
2376 }
2377
2378
2379 bool BufferParams::addLayoutModule(string const & modName)
2380 {
2381         LayoutModuleList::const_iterator it = layout_modules_.begin();
2382         LayoutModuleList::const_iterator end = layout_modules_.end();
2383         for (; it != end; ++it)
2384                 if (*it == modName)
2385                         return false;
2386         layout_modules_.push_back(modName);
2387         return true;
2388 }
2389
2390
2391 string BufferParams::bufferFormat() const
2392 {
2393         return documentClass().outputFormat();
2394 }
2395
2396
2397 bool BufferParams::isExportable(string const & format, bool need_viewable) const
2398 {
2399         FormatList const & formats = exportableFormats(need_viewable);
2400         FormatList::const_iterator fit = formats.begin();
2401         FormatList::const_iterator end = formats.end();
2402         for (; fit != end ; ++fit) {
2403                 if ((*fit)->name() == format)
2404                         return true;
2405         }
2406         return false;
2407 }
2408
2409
2410 FormatList const & BufferParams::exportableFormats(bool only_viewable) const
2411 {
2412         FormatList & cached = only_viewable ?
2413                         pimpl_->viewableFormatList : pimpl_->exportableFormatList;
2414         bool & valid = only_viewable ? 
2415                         pimpl_->isViewCacheValid : pimpl_->isExportCacheValid;
2416         if (valid)
2417                 return cached;
2418
2419         vector<string> const backs = backends();
2420         set<string> excludes;
2421         if (useNonTeXFonts) {
2422                 excludes.insert("latex");
2423                 excludes.insert("pdflatex");
2424         }
2425         FormatList result =
2426                 theConverters().getReachable(backs[0], only_viewable, true, excludes);
2427         for (vector<string>::const_iterator it = backs.begin() + 1;
2428              it != backs.end(); ++it) {
2429                 FormatList r = theConverters().getReachable(*it, only_viewable, 
2430                                 false, excludes);
2431                 result.insert(result.end(), r.begin(), r.end());
2432         }
2433         sort(result.begin(), result.end(), Format::formatSorter);
2434         cached = result;
2435         valid = true;
2436         return cached;
2437 }
2438
2439
2440 vector<string> BufferParams::backends() const
2441 {
2442         vector<string> v;
2443         string const buffmt = bufferFormat();
2444
2445         // FIXME: Don't hardcode format names here, but use a flag
2446         if (buffmt == "latex") {
2447                 if (encoding().package() == Encoding::japanese)
2448                         v.push_back("platex");
2449                 else {
2450                         if (!useNonTeXFonts) {
2451                                 v.push_back("pdflatex");
2452                                 v.push_back("latex");
2453                         }
2454                         v.push_back("xetex");
2455                         v.push_back("luatex");
2456                         v.push_back("dviluatex");
2457                 }
2458         } else
2459                 v.push_back(buffmt);
2460
2461         v.push_back("xhtml");
2462         v.push_back("text");
2463         v.push_back("lyx");
2464         return v;
2465 }
2466
2467
2468 OutputParams::FLAVOR BufferParams::getOutputFlavor(string const & format) const
2469 {
2470         string const dformat = (format.empty() || format == "default") ?
2471                 getDefaultOutputFormat() : format;
2472         DefaultFlavorCache::const_iterator it =
2473                 default_flavors_.find(dformat);
2474
2475         if (it != default_flavors_.end())
2476                 return it->second;
2477
2478         OutputParams::FLAVOR result = OutputParams::LATEX;
2479
2480         // FIXME It'd be better not to hardcode this, but to do
2481         //       something with formats.
2482         if (dformat == "xhtml")
2483                 result = OutputParams::HTML;
2484         else if (dformat == "text")
2485                 result = OutputParams::TEXT;
2486         else if (dformat == "lyx")
2487                 result = OutputParams::LYX;
2488         else if (dformat == "pdflatex")
2489                 result = OutputParams::PDFLATEX;
2490         else if (dformat == "xetex")
2491                 result = OutputParams::XETEX;
2492         else if (dformat == "luatex")
2493                 result = OutputParams::LUATEX;
2494         else if (dformat == "dviluatex")
2495                 result = OutputParams::DVILUATEX;
2496         else {
2497                 // Try to determine flavor of default output format
2498                 vector<string> backs = backends();
2499                 if (find(backs.begin(), backs.end(), dformat) == backs.end()) {
2500                         // Get shortest path to format
2501                         Graph::EdgePath path;
2502                         for (vector<string>::const_iterator it = backs.begin();
2503                             it != backs.end(); ++it) {
2504                                 Graph::EdgePath p = theConverters().getPath(*it, dformat);
2505                                 if (!p.empty() && (path.empty() || p.size() < path.size())) {
2506                                         path = p;
2507                                 }
2508                         }
2509                         if (!path.empty())
2510                                 result = theConverters().getFlavor(path);
2511                 }
2512         }
2513         // cache this flavor
2514         default_flavors_[dformat] = result;
2515         return result;
2516 }
2517
2518
2519 string BufferParams::getDefaultOutputFormat() const
2520 {
2521         if (!default_output_format.empty()
2522             && default_output_format != "default")
2523                 return default_output_format;
2524         if (isDocBook()
2525             || encoding().package() == Encoding::japanese) {
2526                 FormatList const & formats = exportableFormats(true);
2527                 if (formats.empty())
2528                         return string();
2529                 // return the first we find
2530                 return formats.front()->name();
2531         }
2532         if (useNonTeXFonts)
2533                 return lyxrc.default_otf_view_format;
2534         return lyxrc.default_view_format;
2535 }
2536
2537 Font const BufferParams::getFont() const
2538 {
2539         FontInfo f = documentClass().defaultfont();
2540         if (fonts_default_family == "rmdefault")
2541                 f.setFamily(ROMAN_FAMILY);
2542         else if (fonts_default_family == "sfdefault")
2543                 f.setFamily(SANS_FAMILY);
2544         else if (fonts_default_family == "ttdefault")
2545                 f.setFamily(TYPEWRITER_FAMILY);
2546         return Font(f, language);
2547 }
2548
2549
2550 InsetQuotes::QuoteLanguage BufferParams::getQuoteStyle(string const & qs) const
2551 {
2552         return quoteslangtranslator().find(qs);
2553 }
2554
2555
2556 bool BufferParams::isLatex() const
2557 {
2558         return documentClass().outputType() == LATEX;
2559 }
2560
2561
2562 bool BufferParams::isLiterate() const
2563 {
2564         return documentClass().outputType() == LITERATE;
2565 }
2566
2567
2568 bool BufferParams::isDocBook() const
2569 {
2570         return documentClass().outputType() == DOCBOOK;
2571 }
2572
2573
2574 void BufferParams::readPreamble(Lexer & lex)
2575 {
2576         if (lex.getString() != "\\begin_preamble")
2577                 lyxerr << "Error (BufferParams::readPreamble):"
2578                         "consistency check failed." << endl;
2579
2580         preamble = lex.getLongString(from_ascii("\\end_preamble"));
2581 }
2582
2583
2584 void BufferParams::readLocalLayout(Lexer & lex, bool forced)
2585 {
2586         string const expected = forced ? "\\begin_forced_local_layout" :
2587                                          "\\begin_local_layout";
2588         if (lex.getString() != expected)
2589                 lyxerr << "Error (BufferParams::readLocalLayout):"
2590                         "consistency check failed." << endl;
2591
2592         if (forced)
2593                 forced_local_layout_ =
2594                         lex.getLongString(from_ascii("\\end_forced_local_layout"));
2595         else
2596                 local_layout_ = lex.getLongString(from_ascii("\\end_local_layout"));
2597 }
2598
2599
2600 bool BufferParams::setLanguage(string const & lang)
2601 {
2602         Language const *new_language = languages.getLanguage(lang);
2603         if (!new_language) {
2604                 // Language lang was not found
2605                 return false;
2606         }
2607         language = new_language;
2608         return true;
2609 }
2610
2611
2612 void BufferParams::readLanguage(Lexer & lex)
2613 {
2614         if (!lex.next()) return;
2615
2616         string const tmptok = lex.getString();
2617
2618         // check if tmptok is part of tex_babel in tex-defs.h
2619         if (!setLanguage(tmptok)) {
2620                 // Language tmptok was not found
2621                 language = default_language;
2622                 lyxerr << "Warning: Setting language `"
2623                        << tmptok << "' to `" << language->lang()
2624                        << "'." << endl;
2625         }
2626 }
2627
2628
2629 void BufferParams::readGraphicsDriver(Lexer & lex)
2630 {
2631         if (!lex.next())
2632                 return;
2633
2634         string const tmptok = lex.getString();
2635         // check if tmptok is part of tex_graphics in tex_defs.h
2636         int n = 0;
2637         while (true) {
2638                 string const test = tex_graphics[n++];
2639
2640                 if (test == tmptok) {
2641                         graphics_driver = tmptok;
2642                         break;
2643                 }
2644                 if (test.empty()) {
2645                         lex.printError(
2646                                 "Warning: graphics driver `$$Token' not recognized!\n"
2647                                 "         Setting graphics driver to `default'.\n");
2648                         graphics_driver = "default";
2649                         break;
2650                 }
2651         }
2652 }
2653
2654
2655 void BufferParams::readBullets(Lexer & lex)
2656 {
2657         if (!lex.next())
2658                 return;
2659
2660         int const index = lex.getInteger();
2661         lex.next();
2662         int temp_int = lex.getInteger();
2663         user_defined_bullet(index).setFont(temp_int);
2664         temp_bullet(index).setFont(temp_int);
2665         lex >> temp_int;
2666         user_defined_bullet(index).setCharacter(temp_int);
2667         temp_bullet(index).setCharacter(temp_int);
2668         lex >> temp_int;
2669         user_defined_bullet(index).setSize(temp_int);
2670         temp_bullet(index).setSize(temp_int);
2671 }
2672
2673
2674 void BufferParams::readBulletsLaTeX(Lexer & lex)
2675 {
2676         // The bullet class should be able to read this.
2677         if (!lex.next())
2678                 return;
2679         int const index = lex.getInteger();
2680         lex.next(true);
2681         docstring const temp_str = lex.getDocString();
2682
2683         user_defined_bullet(index).setText(temp_str);
2684         temp_bullet(index).setText(temp_str);
2685 }
2686
2687
2688 void BufferParams::readModules(Lexer & lex)
2689 {
2690         if (!lex.eatLine()) {
2691                 lyxerr << "Error (BufferParams::readModules):"
2692                                 "Unexpected end of input." << endl;
2693                 return;
2694         }
2695         while (true) {
2696                 string mod = lex.getString();
2697                 if (mod == "\\end_modules")
2698                         break;
2699                 addLayoutModule(mod);
2700                 lex.eatLine();
2701         }
2702 }
2703
2704
2705 void BufferParams::readRemovedModules(Lexer & lex)
2706 {
2707         if (!lex.eatLine()) {
2708                 lyxerr << "Error (BufferParams::readRemovedModules):"
2709                                 "Unexpected end of input." << endl;
2710                 return;
2711         }
2712         while (true) {
2713                 string mod = lex.getString();
2714                 if (mod == "\\end_removed_modules")
2715                         break;
2716                 removed_modules_.push_back(mod);
2717                 lex.eatLine();
2718         }
2719         // now we want to remove any removed modules that were previously
2720         // added. normally, that will be because default modules were added in
2721         // setBaseClass(), which gets called when \textclass is read at the
2722         // start of the read.
2723         list<string>::const_iterator rit = removed_modules_.begin();
2724         list<string>::const_iterator const ren = removed_modules_.end();
2725         for (; rit != ren; ++rit) {
2726                 LayoutModuleList::iterator const mit = layout_modules_.begin();
2727                 LayoutModuleList::iterator const men = layout_modules_.end();
2728                 LayoutModuleList::iterator found = find(mit, men, *rit);
2729                 if (found == men)
2730                         continue;
2731                 layout_modules_.erase(found);
2732         }
2733 }
2734
2735
2736 void BufferParams::readIncludeonly(Lexer & lex)
2737 {
2738         if (!lex.eatLine()) {
2739                 lyxerr << "Error (BufferParams::readIncludeonly):"
2740                                 "Unexpected end of input." << endl;
2741                 return;
2742         }
2743         while (true) {
2744                 string child = lex.getString();
2745                 if (child == "\\end_includeonly")
2746                         break;
2747                 included_children_.push_back(child);
2748                 lex.eatLine();
2749         }
2750 }
2751
2752
2753 string BufferParams::paperSizeName(PapersizePurpose purpose) const
2754 {
2755         switch (papersize) {
2756         case PAPER_DEFAULT:
2757                 // could be anything, so don't guess
2758                 return string();
2759         case PAPER_CUSTOM: {
2760                 if (purpose == XDVI && !paperwidth.empty() &&
2761                     !paperheight.empty()) {
2762                         // heightxwidth<unit>
2763                         string first = paperwidth;
2764                         string second = paperheight;
2765                         if (orientation == ORIENTATION_LANDSCAPE)
2766                                 first.swap(second);
2767                         // cut off unit.
2768                         return first.erase(first.length() - 2)
2769                                 + "x" + second;
2770                 }
2771                 return string();
2772         }
2773         case PAPER_A0:
2774                 // dvips and dvipdfm do not know this
2775                 if (purpose == DVIPS || purpose == DVIPDFM)
2776                         return string();
2777                 return "a0";
2778         case PAPER_A1:
2779                 if (purpose == DVIPS || purpose == DVIPDFM)
2780                         return string();
2781                 return "a1";
2782         case PAPER_A2:
2783                 if (purpose == DVIPS || purpose == DVIPDFM)
2784                         return string();
2785                 return "a2";
2786         case PAPER_A3:
2787                 return "a3";
2788         case PAPER_A4:
2789                 return "a4";
2790         case PAPER_A5:
2791                 return "a5";
2792         case PAPER_A6:
2793                 if (purpose == DVIPS || purpose == DVIPDFM)
2794                         return string();
2795                 return "a6";
2796         case PAPER_B0:
2797                 if (purpose == DVIPS || purpose == DVIPDFM)
2798                         return string();
2799                 return "b0";
2800         case PAPER_B1:
2801                 if (purpose == DVIPS || purpose == DVIPDFM)
2802                         return string();
2803                 return "b1";
2804         case PAPER_B2:
2805                 if (purpose == DVIPS || purpose == DVIPDFM)
2806                         return string();
2807                 return "b2";
2808         case PAPER_B3:
2809                 if (purpose == DVIPS || purpose == DVIPDFM)
2810                         return string();
2811                 return "b3";
2812         case PAPER_B4:
2813                 // dvipdfm does not know this
2814                 if (purpose == DVIPDFM)
2815                         return string();
2816                 return "b4";
2817         case PAPER_B5:
2818                 if (purpose == DVIPDFM)
2819                         return string();
2820                 return "b5";
2821         case PAPER_B6:
2822                 if (purpose == DVIPS || purpose == DVIPDFM)
2823                         return string();
2824                 return "b6";
2825         case PAPER_C0:
2826                 if (purpose == DVIPS || purpose == DVIPDFM)
2827                         return string();
2828                 return "c0";
2829         case PAPER_C1:
2830                 if (purpose == DVIPS || purpose == DVIPDFM)
2831                         return string();
2832                 return "c1";
2833         case PAPER_C2:
2834                 if (purpose == DVIPS || purpose == DVIPDFM)
2835                         return string();
2836                 return "c2";
2837         case PAPER_C3:
2838                 if (purpose == DVIPS || purpose == DVIPDFM)
2839                         return string();
2840                 return "c3";
2841         case PAPER_C4:
2842                 if (purpose == DVIPS || purpose == DVIPDFM)
2843                         return string();
2844                 return "c4";
2845         case PAPER_C5:
2846                 if (purpose == DVIPS || purpose == DVIPDFM)
2847                         return string();
2848                 return "c5";
2849         case PAPER_C6:
2850                 if (purpose == DVIPS || purpose == DVIPDFM)
2851                         return string();
2852                 return "c6";
2853         case PAPER_JISB0:
2854                 if (purpose == DVIPS || purpose == DVIPDFM)
2855                         return string();
2856                 return "jisb0";
2857         case PAPER_JISB1:
2858                 if (purpose == DVIPS || purpose == DVIPDFM)
2859                         return string();
2860                 return "jisb1";
2861         case PAPER_JISB2:
2862                 if (purpose == DVIPS || purpose == DVIPDFM)
2863                         return string();
2864                 return "jisb2";
2865         case PAPER_JISB3:
2866                 if (purpose == DVIPS || purpose == DVIPDFM)
2867                         return string();
2868                 return "jisb3";
2869         case PAPER_JISB4:
2870                 if (purpose == DVIPS || purpose == DVIPDFM)
2871                         return string();
2872                 return "jisb4";
2873         case PAPER_JISB5:
2874                 if (purpose == DVIPS || purpose == DVIPDFM)
2875                         return string();
2876                 return "jisb5";
2877         case PAPER_JISB6:
2878                 if (purpose == DVIPS || purpose == DVIPDFM)
2879                         return string();
2880                 return "jisb6";
2881         case PAPER_USEXECUTIVE:
2882                 // dvipdfm does not know this
2883                 if (purpose == DVIPDFM)
2884                         return string();
2885                 return "foolscap";
2886         case PAPER_USLEGAL:
2887                 return "legal";
2888         case PAPER_USLETTER:
2889         default:
2890                 if (purpose == XDVI)
2891                         return "us";
2892                 return "letter";
2893         }
2894 }
2895
2896
2897 string const BufferParams::dvips_options() const
2898 {
2899         string result;
2900
2901         // If the class loads the geometry package, we do not know which
2902         // paper size is used, since we do not set it (bug 7013).
2903         // Therefore we must not specify any argument here.
2904         // dvips gets the correct paper size via DVI specials in this case
2905         // (if the class uses the geometry package correctly).
2906         if (documentClass().provides("geometry"))
2907                 return result;
2908
2909         if (use_geometry
2910             && papersize == PAPER_CUSTOM
2911             && !lyxrc.print_paper_dimension_flag.empty()
2912             && !paperwidth.empty()
2913             && !paperheight.empty()) {
2914                 // using a custom papersize
2915                 result = lyxrc.print_paper_dimension_flag;
2916                 result += ' ' + paperwidth;
2917                 result += ',' + paperheight;
2918         } else {
2919                 string const paper_option = paperSizeName(DVIPS);
2920                 if (!paper_option.empty() && (paper_option != "letter" ||
2921                     orientation != ORIENTATION_LANDSCAPE)) {
2922                         // dvips won't accept -t letter -t landscape.
2923                         // In all other cases, include the paper size
2924                         // explicitly.
2925                         result = lyxrc.print_paper_flag;
2926                         result += ' ' + paper_option;
2927                 }
2928         }
2929         if (orientation == ORIENTATION_LANDSCAPE &&
2930             papersize != PAPER_CUSTOM)
2931                 result += ' ' + lyxrc.print_landscape_flag;
2932         return result;
2933 }
2934
2935
2936 string const BufferParams::font_encoding() const
2937 {
2938         return font_encodings().empty() ? "default" : font_encodings().back();
2939 }
2940
2941
2942 vector<string> const BufferParams::font_encodings() const
2943 {
2944         string doc_fontenc = (fontenc == "global") ? lyxrc.fontenc : fontenc;
2945
2946         vector<string> fontencs;
2947
2948         // "default" means "no explicit font encoding"
2949         if (doc_fontenc != "default") {
2950                 fontencs = getVectorFromString(doc_fontenc);
2951                 if (!language->fontenc().empty()
2952                     && ascii_lowercase(language->fontenc()) != "none") {
2953                         vector<string> fencs = getVectorFromString(language->fontenc());
2954                         vector<string>::const_iterator fit = fencs.begin();
2955                         for (; fit != fencs.end(); ++fit) {
2956                                 if (find(fontencs.begin(), fontencs.end(), *fit) == fontencs.end())
2957                                         fontencs.push_back(*fit);
2958                         }
2959                 }
2960         }
2961
2962         return fontencs;
2963 }
2964
2965
2966 string BufferParams::babelCall(string const & lang_opts, bool const langoptions) const
2967 {
2968         // suppress the babel call if there is no BabelName defined
2969         // for the document language in the lib/languages file and if no
2970         // other languages are used (lang_opts is then empty)
2971         if (lang_opts.empty())
2972                 return string();
2973         // either a specific language (AsBabelOptions setting in
2974         // lib/languages) or the prefs require the languages to
2975         // be submitted to babel itself (not the class).
2976         if (langoptions)
2977                 return "\\usepackage[" + lang_opts + "]{babel}";
2978         return "\\usepackage{babel}";
2979 }
2980
2981
2982 docstring BufferParams::getGraphicsDriver(string const & package) const
2983 {
2984         docstring result;
2985
2986         if (package == "geometry") {
2987                 if (graphics_driver == "dvips"
2988                     || graphics_driver == "dvipdfm"
2989                     || graphics_driver == "pdftex"
2990                     || graphics_driver == "vtex")
2991                         result = from_ascii(graphics_driver);
2992                 else if (graphics_driver == "dvipdfmx")
2993                         result = from_ascii("dvipdfm");
2994         }
2995
2996         return result;
2997 }
2998
2999
3000 void BufferParams::writeEncodingPreamble(otexstream & os,
3001                                          LaTeXFeatures & features) const
3002 {
3003         // XeTeX/LuaTeX: (see also #9740)
3004         // With Unicode fonts we use utf8-plain without encoding package.
3005         // With TeX fonts, we cannot use utf8-plain, but "inputenc" fails.
3006         // XeTeX must use ASCII encoding (see Buffer.cpp),
3007         //  for LuaTeX, we load "luainputenc" (see below).
3008         if (useNonTeXFonts || features.runparams().flavor == OutputParams::XETEX)
3009                 return;
3010
3011         if (inputenc == "auto") {
3012                 string const doc_encoding =
3013                         language->encoding()->latexName();
3014                 Encoding::Package const package =
3015                         language->encoding()->package();
3016
3017                 // Create list of inputenc options:
3018                 set<string> encodings;
3019                 // luainputenc fails with more than one encoding
3020                 if (!features.runparams().isFullUnicode()) // if we reach this point, this means LuaTeX with TeX fonts
3021                         // list all input encodings used in the document
3022                         encodings = features.getEncodingSet(doc_encoding);
3023
3024                 // If the "japanese" package (i.e. pLaTeX) is used,
3025                 // inputenc must be omitted.
3026                 // see http://www.mail-archive.com/lyx-devel@lists.lyx.org/msg129680.html
3027                 if ((!encodings.empty() || package == Encoding::inputenc)
3028                     && !features.isRequired("japanese")
3029                     && !features.isProvided("inputenc")) {
3030                         os << "\\usepackage[";
3031                         set<string>::const_iterator it = encodings.begin();
3032                         set<string>::const_iterator const end = encodings.end();
3033                         if (it != end) {
3034                                 os << from_ascii(*it);
3035                                 ++it;
3036                         }
3037                         for (; it != end; ++it)
3038                                 os << ',' << from_ascii(*it);
3039                         if (package == Encoding::inputenc) {
3040                                 if (!encodings.empty())
3041                                         os << ',';
3042                                 os << from_ascii(doc_encoding);
3043                         }
3044                         if (features.runparams().flavor == OutputParams::LUATEX
3045                             || features.runparams().flavor == OutputParams::DVILUATEX)
3046                                 os << "]{luainputenc}\n";
3047                         else
3048                                 os << "]{inputenc}\n";
3049                 }
3050                 if (package == Encoding::CJK || features.mustProvide("CJK")) {
3051                         if (language->encoding()->name() == "utf8-cjk"
3052                             && LaTeXFeatures::isAvailable("CJKutf8"))
3053                                 os << "\\usepackage{CJKutf8}\n";
3054                         else
3055                                 os << "\\usepackage{CJK}\n";
3056                 }
3057         } else if (inputenc != "default") {
3058                 switch (encoding().package()) {
3059                 case Encoding::none:
3060                 case Encoding::japanese:
3061                         break;
3062                 case Encoding::inputenc:
3063                         // do not load inputenc if japanese is used
3064                         // or if the class provides inputenc
3065                         if (features.isRequired("japanese")
3066                             || features.isProvided("inputenc"))
3067                                 break;
3068                         os << "\\usepackage[" << from_ascii(encoding().latexName());
3069                         if (features.runparams().flavor == OutputParams::LUATEX
3070                             || features.runparams().flavor == OutputParams::DVILUATEX)
3071                                 os << "]{luainputenc}\n";
3072                         else
3073                                 os << "]{inputenc}\n";
3074                         break;
3075                 case Encoding::CJK:
3076                         if (encoding().name() == "utf8-cjk"
3077                             && LaTeXFeatures::isAvailable("CJKutf8"))
3078                                 os << "\\usepackage{CJKutf8}\n";
3079                         else
3080                                 os << "\\usepackage{CJK}\n";
3081                         break;
3082                 }
3083                 // Load the CJK package if needed by a secondary language.
3084                 // If the main encoding is some variant of UTF8, use CJKutf8.
3085                 if (encoding().package() != Encoding::CJK && features.mustProvide("CJK")) {
3086                         if (encoding().iconvName() == "UTF-8"
3087                             && LaTeXFeatures::isAvailable("CJKutf8"))
3088                                 os << "\\usepackage{CJKutf8}\n";
3089                         else
3090                                 os << "\\usepackage{CJK}\n";
3091                 }
3092         }
3093 }
3094
3095
3096 string const BufferParams::parseFontName(string const & name) const
3097 {
3098         string mangled = name;
3099         size_t const idx = mangled.find('[');
3100         if (idx == string::npos || idx == 0)
3101                 return mangled;
3102         else
3103                 return mangled.substr(0, idx - 1);
3104 }
3105
3106
3107 string const BufferParams::loadFonts(LaTeXFeatures & features) const
3108 {
3109         if (fontsRoman() == "default" && fontsSans() == "default"
3110             && fontsTypewriter() == "default"
3111             && (fontsMath() == "default" || fontsMath() == "auto"))
3112                 //nothing to do
3113                 return string();
3114
3115         ostringstream os;
3116
3117         /* Fontspec (XeTeX, LuaTeX): we provide GUI support for oldstyle
3118          * numbers (Numbers=OldStyle) and sf/tt scaling. The Ligatures=TeX/
3119          * Mapping=tex-text option assures TeX ligatures (such as "--")
3120          * are resolved. Note that tt does not use these ligatures.
3121          * TODO:
3122          *    -- add more GUI options?
3123          *    -- add more fonts (fonts for other scripts)
3124          *    -- if there's a way to find out if a font really supports
3125          *       OldStyle, enable/disable the widget accordingly.
3126         */
3127         if (useNonTeXFonts && features.isAvailable("fontspec")) {
3128                 // "Mapping=tex-text" and "Ligatures=TeX" are equivalent.
3129                 // However, until v.2 (2010/07/11) fontspec only knew
3130                 // Mapping=tex-text (for XeTeX only); then "Ligatures=TeX"
3131                 // was introduced for both XeTeX and LuaTeX (LuaTeX
3132                 // didn't understand "Mapping=tex-text", while XeTeX
3133                 // understood both. With most recent versions, both
3134                 // variants are understood by both engines. However,
3135                 // we want to provide support for at least TeXLive 2009
3136                 // (for XeTeX; LuaTeX is only supported as of v.2)
3137                 string const texmapping =
3138                         (features.runparams().flavor == OutputParams::XETEX) ?
3139                         "Mapping=tex-text" : "Ligatures=TeX";
3140                 if (fontsRoman() != "default") {
3141                         os << "\\setmainfont[" << texmapping;
3142                         if (fonts_old_figures)
3143                                 os << ",Numbers=OldStyle";
3144                         os << "]{" << parseFontName(fontsRoman()) << "}\n";
3145                 }
3146                 if (fontsSans() != "default") {
3147                         string const sans = parseFontName(fontsSans());
3148                         if (fontsSansScale() != 100)
3149                                 os << "\\setsansfont[Scale="
3150                                    << float(fontsSansScale()) / 100
3151                                    << "," << texmapping << "]{"
3152                                    << sans << "}\n";
3153                         else
3154                                 os << "\\setsansfont[" << texmapping << "]{"
3155                                    << sans << "}\n";
3156                 }
3157                 if (fontsTypewriter() != "default") {
3158                         string const mono = parseFontName(fontsTypewriter());
3159                         if (fontsTypewriterScale() != 100)
3160                                 os << "\\setmonofont[Scale="
3161                                    << float(fontsTypewriterScale()) / 100
3162                                    << "]{"
3163                                    << mono << "}\n";
3164                         else
3165                                 os << "\\setmonofont{"
3166                                    << mono << "}\n";
3167                 }
3168                 return os.str();
3169         }
3170
3171         // Tex Fonts
3172         bool const ot1 = (font_encoding() == "default" || font_encoding() == "OT1");
3173         bool const dryrun = features.runparams().dryrun;
3174         bool const complete = (fontsSans() == "default" && fontsTypewriter() == "default");
3175         bool const nomath = (fontsMath() == "default");
3176
3177         // ROMAN FONTS
3178         os << theLaTeXFonts().getLaTeXFont(from_ascii(fontsRoman())).getLaTeXCode(
3179                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
3180                 nomath);
3181
3182         // SANS SERIF
3183         os << theLaTeXFonts().getLaTeXFont(from_ascii(fontsSans())).getLaTeXCode(
3184                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
3185                 nomath, fontsSansScale());
3186
3187         // MONOSPACED/TYPEWRITER
3188         os << theLaTeXFonts().getLaTeXFont(from_ascii(fontsTypewriter())).getLaTeXCode(
3189                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
3190                 nomath, fontsTypewriterScale());
3191
3192         // MATH
3193         os << theLaTeXFonts().getLaTeXFont(from_ascii(fontsMath())).getLaTeXCode(
3194                 dryrun, ot1, complete, fonts_expert_sc, fonts_old_figures,
3195                 nomath);
3196
3197         return os.str();
3198 }
3199
3200
3201 Encoding const & BufferParams::encoding() const
3202 {
3203         // Main encoding for LaTeX output.
3204         // 
3205         // Exception: XeTeX with 8-bit TeX fonts requires ASCII (see #9740).
3206         // As the "flavor" is only known once export started, this
3207         // cannot be handled here. Instead, runparams.encoding is set
3208         // to ASCII in Buffer::makeLaTeXFile (for export)
3209         // and Buffer::writeLaTeXSource (for preview).
3210         if (useNonTeXFonts)
3211                 return *(encodings.fromLyXName("utf8-plain"));
3212         if (inputenc == "auto" || inputenc == "default")
3213                 return *language->encoding();
3214         Encoding const * const enc = encodings.fromLyXName(inputenc);
3215         if (enc)
3216                 return *enc;
3217         LYXERR0("Unknown inputenc value `" << inputenc
3218                << "'. Using `auto' instead.");
3219         return *language->encoding();
3220 }
3221
3222
3223 bool BufferParams::addCiteEngine(string const & engine)
3224 {
3225         LayoutModuleList::const_iterator it = cite_engine_.begin();
3226         LayoutModuleList::const_iterator en = cite_engine_.end();
3227         for (; it != en; ++it)
3228                 if (*it == engine)
3229                         return false;
3230         cite_engine_.push_back(engine);
3231         return true;
3232 }
3233
3234
3235 bool BufferParams::addCiteEngine(vector<string> const & engine)
3236 {
3237         vector<string>::const_iterator it = engine.begin();
3238         vector<string>::const_iterator en = engine.end();
3239         bool ret = true;
3240         for (; it != en; ++it)
3241                 if (!addCiteEngine(*it))
3242                         ret = false;
3243         return ret;
3244 }
3245
3246
3247 string const & BufferParams::defaultBiblioStyle() const
3248 {
3249         return documentClass().defaultBiblioStyle();
3250 }
3251
3252
3253 bool const & BufferParams::fullAuthorList() const
3254 {
3255         return documentClass().fullAuthorList();
3256 }
3257
3258
3259 void BufferParams::setCiteEngine(string const & engine)
3260 {
3261         clearCiteEngine();
3262         addCiteEngine(engine);
3263 }
3264
3265
3266 void BufferParams::setCiteEngine(vector<string> const & engine)
3267 {
3268         clearCiteEngine();
3269         addCiteEngine(engine);
3270 }
3271
3272
3273 vector<string> BufferParams::citeCommands() const
3274 {
3275         static CitationStyle const default_style;
3276         vector<string> commands =
3277                 documentClass().citeCommands(citeEngineType());
3278         if (commands.empty())
3279                 commands.push_back(default_style.cmd);
3280         return commands;
3281 }
3282
3283
3284 vector<CitationStyle> BufferParams::citeStyles() const
3285 {
3286         static CitationStyle const default_style;
3287         vector<CitationStyle> styles =
3288                 documentClass().citeStyles(citeEngineType());
3289         if (styles.empty())
3290                 styles.push_back(default_style);
3291         return styles;
3292 }
3293
3294 void BufferParams::invalidateConverterCache() const
3295 {
3296         pimpl_->isExportCacheValid = false;
3297         pimpl_->isViewCacheValid = false;
3298 }
3299
3300 } // namespace lyx