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