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