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