]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
Whitespace cleanup.
[lyx.git] / src / bufferparams.C
1 /**
2  * \file bufferparams.C
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 "BranchList.h"
22 #include "Bullet.h"
23 #include "debug.h"
24 #include "encoding.h"
25 #include "gettext.h"
26 #include "language.h"
27 #include "LaTeXFeatures.h"
28 #include "LColor.h"
29 #include "lyxlex.h"
30 #include "lyxrc.h"
31 #include "lyxtextclasslist.h"
32 #include "outputparams.h"
33 #include "tex-strings.h"
34 #include "Spacing.h"
35 #include "texrow.h"
36 #include "vspace.h"
37
38 #include "frontends/Alert.h"
39
40 #include "support/lyxalgo.h" // for lyx::count
41 #include "support/convert.h"
42 #include "support/translator.h"
43
44 #include <boost/array.hpp>
45
46 #include <sstream>
47
48 namespace support = lyx::support;
49 using lyx::support::bformat;
50 using lyx::support::rtrim;
51 using lyx::support::tokenPos;
52
53 using std::endl;
54 using std::string;
55 using std::istringstream;
56 using std::ostream;
57 using std::ostringstream;
58 using std::pair;
59
60 namespace biblio = lyx::biblio;
61
62
63 // Local translators
64 namespace {
65
66 // Paragraph separation
67 typedef Translator<string, BufferParams::PARSEP> ParSepTranslator;
68
69
70 ParSepTranslator const init_parseptranslator()
71 {
72         ParSepTranslator translator(string_paragraph_separation[0], BufferParams::PARSEP_INDENT);
73         translator.addPair(string_paragraph_separation[1], BufferParams::PARSEP_SKIP);
74         return translator;
75 }
76
77
78 ParSepTranslator const & parseptranslator()
79 {
80         static ParSepTranslator translator = init_parseptranslator();
81         return translator;
82 }
83
84
85 // Quotes language
86 typedef Translator<string, InsetQuotes::quote_language> QuotesLangTranslator;
87
88
89 QuotesLangTranslator const init_quoteslangtranslator()
90 {
91         QuotesLangTranslator translator(string_quotes_language[0], InsetQuotes::EnglishQ);
92         translator.addPair(string_quotes_language[1], InsetQuotes::SwedishQ);
93         translator.addPair(string_quotes_language[2], InsetQuotes::GermanQ);
94         translator.addPair(string_quotes_language[3], InsetQuotes::PolishQ);
95         translator.addPair(string_quotes_language[4], InsetQuotes::FrenchQ);
96         translator.addPair(string_quotes_language[5], InsetQuotes::DanishQ);
97         return translator;
98 }
99
100
101 QuotesLangTranslator const & quoteslangtranslator()
102 {
103         static QuotesLangTranslator translator = init_quoteslangtranslator();
104         return translator;
105 }
106
107
108 // Paper size
109 typedef Translator<std::string, PAPER_SIZE> PaperSizeTranslator;
110
111
112 PaperSizeTranslator const init_papersizetranslator()
113 {
114         PaperSizeTranslator translator(string_papersize[0], PAPER_DEFAULT);
115         translator.addPair(string_papersize[1], PAPER_CUSTOM);
116         translator.addPair(string_papersize[2], PAPER_USLETTER);
117         translator.addPair(string_papersize[3], PAPER_USLEGAL);
118         translator.addPair(string_papersize[4], PAPER_USEXECUTIVE);
119         translator.addPair(string_papersize[5], PAPER_A3);
120         translator.addPair(string_papersize[6], PAPER_A4);
121         translator.addPair(string_papersize[7], PAPER_A5);
122         translator.addPair(string_papersize[8], PAPER_B3);
123         translator.addPair(string_papersize[9], PAPER_B4);
124         translator.addPair(string_papersize[10], PAPER_B5);
125         return translator;
126 }
127
128
129 PaperSizeTranslator const & papersizetranslator()
130 {
131         static PaperSizeTranslator translator = init_papersizetranslator();
132         return translator;
133 }
134
135
136 // Paper orientation
137 typedef Translator<string, PAPER_ORIENTATION> PaperOrientationTranslator;
138
139
140 PaperOrientationTranslator const init_paperorientationtranslator()
141 {
142         PaperOrientationTranslator translator(string_orientation[0], ORIENTATION_PORTRAIT);
143         translator.addPair(string_orientation[1], ORIENTATION_LANDSCAPE);
144         return translator;
145 }
146
147
148 PaperOrientationTranslator const & paperorientationtranslator()
149 {
150         static PaperOrientationTranslator translator = init_paperorientationtranslator();
151         return translator;
152 }
153
154
155 // Page sides
156 typedef Translator<int, LyXTextClass::PageSides> SidesTranslator;
157
158
159 SidesTranslator const init_sidestranslator()
160 {
161         SidesTranslator translator(1, LyXTextClass::OneSide);
162         translator.addPair(2, LyXTextClass::TwoSides);
163         return translator;
164 }
165
166
167 SidesTranslator const & sidestranslator()
168 {
169         static SidesTranslator translator = init_sidestranslator();
170         return translator;
171 }
172
173
174
175 // AMS
176 typedef Translator<int, BufferParams::AMS> AMSTranslator;
177
178
179 AMSTranslator const init_amstranslator()
180 {
181         AMSTranslator translator(0, BufferParams::AMS_OFF);
182         translator.addPair(1, BufferParams::AMS_AUTO);
183         translator.addPair(2, BufferParams::AMS_ON);
184         return translator;
185 }
186
187
188 AMSTranslator const & amstranslator()
189 {
190         static AMSTranslator translator = init_amstranslator();
191         return translator;
192 }
193
194
195 // Cite engine
196 typedef Translator<string, biblio::CiteEngine> CiteEngineTranslator;
197
198
199 CiteEngineTranslator const init_citeenginetranslator()
200 {
201         CiteEngineTranslator translator("basic", biblio::ENGINE_BASIC);
202         translator.addPair("natbib_numerical", biblio::ENGINE_NATBIB_NUMERICAL);
203         translator.addPair("natbib_authoryear", biblio::ENGINE_NATBIB_AUTHORYEAR);
204         translator.addPair("jurabib", biblio::ENGINE_JURABIB);
205         return translator;
206 }
207
208
209 CiteEngineTranslator const & citeenginetranslator()
210 {
211         static CiteEngineTranslator translator = init_citeenginetranslator();
212         return translator;
213 }
214
215
216 // Spacing
217 typedef Translator<string, Spacing::Space> SpaceTranslator;
218
219
220 SpaceTranslator const init_spacetranslator()
221 {
222         SpaceTranslator translator("default", Spacing::Default);
223         translator.addPair("single", Spacing::Single);
224         translator.addPair("onehalf", Spacing::Onehalf);
225         translator.addPair("double", Spacing::Double);
226         translator.addPair("other", Spacing::Other);
227         return translator;
228 }
229
230
231 SpaceTranslator const & spacetranslator()
232 {
233         static SpaceTranslator translator = init_spacetranslator();
234         return translator;
235 }
236
237 // ends annonym namespace
238 }
239
240
241 class BufferParams::Impl
242 {
243 public:
244         Impl();
245
246         AuthorList authorlist;
247         BranchList branchlist;
248         boost::array<Bullet, 4> temp_bullets;
249         boost::array<Bullet, 4> user_defined_bullets;
250         Spacing spacing;
251         /** This is the amount of space used for paragraph_separation "skip",
252          * and for detached paragraphs in "indented" documents.
253          */
254         VSpace defskip;
255 };
256
257
258 BufferParams::Impl::Impl()
259         : defskip(VSpace::MEDSKIP)
260 {
261         // set initial author
262         authorlist.record(Author(lyxrc.user_name, lyxrc.user_email));
263 }
264
265
266 BufferParams::Impl *
267 BufferParams::MemoryTraits::clone(BufferParams::Impl const * ptr)
268 {
269         BOOST_ASSERT(ptr);
270
271         return new BufferParams::Impl(*ptr);
272 }
273
274
275 void BufferParams::MemoryTraits::destroy(BufferParams::Impl * ptr)
276 {
277         delete ptr;
278 }
279
280
281 BufferParams::BufferParams()
282         : // Initialize textclass to point to article. if `first' is
283           // true in the returned pair, then `second' is the textclass
284           // number; if it is false, second is 0. In both cases, second
285           // is what we want.
286         textclass(textclasslist.NumberOfClass("article").second),
287         pimpl_(new Impl)
288 {
289         paragraph_separation = PARSEP_INDENT;
290         quotes_language = InsetQuotes::EnglishQ;
291         fontsize = "default";
292
293         /*  PaperLayout */
294         papersize = PAPER_DEFAULT;
295         orientation = ORIENTATION_PORTRAIT;
296         use_geometry = false;
297         use_amsmath = AMS_AUTO;
298         cite_engine = biblio::ENGINE_BASIC;
299         use_bibtopic = false;
300         tracking_changes = false;
301         output_changes = false;
302         secnumdepth = 3;
303         tocdepth = 3;
304         language = default_language;
305         fonts = "default";
306         inputenc = "auto";
307         graphicsDriver = "default";
308         sides = LyXTextClass::OneSide;
309         columns = 1;
310         pagestyle = "default";
311         compressed = false;
312         for (int iter = 0; iter < 4; ++iter) {
313                 user_defined_bullet(iter) = ITEMIZE_DEFAULTS[iter];
314                 temp_bullet(iter) = ITEMIZE_DEFAULTS[iter];
315         }
316 }
317
318
319 BufferParams::~BufferParams()
320 {}
321
322
323 AuthorList & BufferParams::authors()
324 {
325         return pimpl_->authorlist;
326 }
327
328
329 AuthorList const & BufferParams::authors() const
330 {
331         return pimpl_->authorlist;
332 }
333
334
335 BranchList & BufferParams::branchlist()
336 {
337         return pimpl_->branchlist;
338 }
339
340
341 BranchList const & BufferParams::branchlist() const
342 {
343         return pimpl_->branchlist;
344 }
345
346
347 Bullet & BufferParams::temp_bullet(lyx::size_type const index)
348 {
349         BOOST_ASSERT(index < 4);
350         return pimpl_->temp_bullets[index];
351 }
352
353
354 Bullet const & BufferParams::temp_bullet(lyx::size_type const index) const
355 {
356         BOOST_ASSERT(index < 4);
357         return pimpl_->temp_bullets[index];
358 }
359
360
361 Bullet & BufferParams::user_defined_bullet(lyx::size_type const index)
362 {
363         BOOST_ASSERT(index < 4);
364         return pimpl_->user_defined_bullets[index];
365 }
366
367
368 Bullet const & BufferParams::user_defined_bullet(lyx::size_type const index) const
369 {
370         BOOST_ASSERT(index < 4);
371         return pimpl_->user_defined_bullets[index];
372 }
373
374
375 Spacing & BufferParams::spacing()
376 {
377         return pimpl_->spacing;
378 }
379
380
381 Spacing const & BufferParams::spacing() const
382 {
383         return pimpl_->spacing;
384 }
385
386
387 VSpace const & BufferParams::getDefSkip() const
388 {
389         return pimpl_->defskip;
390 }
391
392
393 void BufferParams::setDefSkip(VSpace const & vs)
394 {
395         pimpl_->defskip = vs;
396 }
397
398
399 string const BufferParams::readToken(LyXLex & lex, string const & token)
400 {
401         if (token == "\\textclass") {
402                 lex.next();
403                 string const classname = lex.getString();
404                 pair<bool, lyx::textclass_type> pp =
405                         textclasslist.NumberOfClass(classname);
406                 if (pp.first) {
407                         textclass = pp.second;
408                 } else {
409                         textclass = 0;
410                         return classname;
411                 }
412                 if (!getLyXTextClass().isTeXClassAvailable()) {
413                         string const msg =
414                                 bformat(_("The document uses a missing "
415                                 "TeX class \"%1$s\".\n"), classname);
416                         Alert::warning(_("Document class not available"),
417                                        msg + _("LyX will not be able to produce output."));
418                 }
419         } else if (token == "\\begin_preamble") {
420                 readPreamble(lex);
421         } else if (token == "\\options") {
422                 lex.eatLine();
423                 options = lex.getString();
424         } else if (token == "\\language") {
425                 readLanguage(lex);
426         } else if (token == "\\inputencoding") {
427                 lex >> inputenc;
428         } else if (token == "\\graphics") {
429                 readGraphicsDriver(lex);
430         } else if (token == "\\fontscheme") {
431                 lex >> fonts;
432         } else if (token == "\\paragraph_separation") {
433                 string parsep;
434                 lex >> parsep;
435                 paragraph_separation = parseptranslator().find(parsep);
436         } else if (token == "\\defskip") {
437                 lex.next();
438                 pimpl_->defskip = VSpace(lex.getString());
439         } else if (token == "\\quotes_language") {
440                 string quotes_lang;
441                 lex >> quotes_lang;
442                 quotes_language = quoteslangtranslator().find(quotes_lang);
443         } else if (token == "\\papersize") {
444                 string ppsize;
445                 lex >> ppsize;
446                 papersize = papersizetranslator().find(ppsize);
447         } else if (token == "\\use_geometry") {
448                 lex >> use_geometry;
449         } else if (token == "\\use_amsmath") {
450                 int use_ams;
451                 lex >> use_ams;
452                 use_amsmath = amstranslator().find(use_ams);
453         } else if (token == "\\cite_engine") {
454                 string engine;
455                 lex >> engine;
456                 cite_engine = citeenginetranslator().find(engine);
457         } else if (token == "\\use_bibtopic") {
458                 lex >> use_bibtopic;
459         } else if (token == "\\tracking_changes") {
460                 lex >> tracking_changes;
461         } else if (token == "\\output_changes") {
462                 lex >> output_changes;
463         } else if (token == "\\branch") {
464                 lex.next();
465                 string branch = lex.getString();
466                 branchlist().add(branch);
467                 while (true) {
468                         lex.next();
469                         string const tok = lex.getString();
470                         if (tok == "\\end_branch")
471                                 break;
472                         Branch * branch_ptr = branchlist().find(branch);
473                         if (tok == "\\selected") {
474                                 lex.next();
475                                 if (branch_ptr)
476                                         branch_ptr->setSelected(lex.getInteger());
477                         }
478                         // not yet operational
479                         if (tok == "\\color") {
480                                 lex.eatLine();
481                                 string color = lex.getString();
482                                 if (branch_ptr)
483                                         branch_ptr->setColor(color);
484                                 // Update also the LColor table:
485                                 if (color == "none")
486                                         color = lcolor.getX11Name(LColor::background);
487                                 lcolor.setColor(branch, color);
488
489                         }
490                 }
491         } else if (token == "\\author") {
492                 lex.eatLine();
493                 istringstream ss(lex.getString());
494                 Author a;
495                 ss >> a;
496                 author_map.push_back(pimpl_->authorlist.record(a));
497         } else if (token == "\\paperorientation") {
498                 string orient;
499                 lex >> orient;
500                 orientation = paperorientationtranslator().find(orient);
501         } else if (token == "\\paperwidth") {
502                 lex >> paperwidth;
503         } else if (token == "\\paperheight") {
504                 lex >> paperheight;
505         } else if (token == "\\leftmargin") {
506                 lex >> leftmargin;
507         } else if (token == "\\topmargin") {
508                 lex >> topmargin;
509         } else if (token == "\\rightmargin") {
510                 lex >> rightmargin;
511         } else if (token == "\\bottommargin") {
512                 lex >> bottommargin;
513         } else if (token == "\\headheight") {
514                 lex >> headheight;
515         } else if (token == "\\headsep") {
516                 lex >> headsep;
517         } else if (token == "\\footskip") {
518                 lex >> footskip;
519         } else if (token == "\\paperfontsize") {
520                 lex >> fontsize;
521         } else if (token == "\\papercolumns") {
522                 lex >> columns;
523         } else if (token == "\\papersides") {
524                 int psides;
525                 lex >> psides;
526                 sides = sidestranslator().find(psides);
527         } else if (token == "\\paperpagestyle") {
528                 lex >> pagestyle;
529         } else if (token == "\\bullet") {
530                 readBullets(lex);
531         } else if (token == "\\bulletLaTeX") {
532                 readBulletsLaTeX(lex);
533         } else if (token == "\\secnumdepth") {
534                 lex >> secnumdepth;
535         } else if (token == "\\tocdepth") {
536                 lex >> tocdepth;
537         } else if (token == "\\spacing") {
538                 string nspacing;
539                 lex >> nspacing;
540                 string tmp_val;
541                 if (nspacing == "other") {
542                         lex >> tmp_val;
543                 }
544                 spacing().set(spacetranslator().find(nspacing), tmp_val);
545         } else if (token == "\\float_placement") {
546                 lex >> float_placement;
547         } else {
548                 return token;
549         }
550
551         return string();
552 }
553
554
555 void BufferParams::writeFile(ostream & os) const
556 {
557         // The top of the file is written by the buffer.
558         // Prints out the buffer info into the .lyx file given by file
559
560         // the textclass
561         os << "\\textclass " << textclasslist[textclass].name() << '\n';
562
563         // then the the preamble
564         if (!preamble.empty()) {
565                 // remove '\n' from the end of preamble
566                 string const tmppreamble = rtrim(preamble, "\n");
567                 os << "\\begin_preamble\n"
568                    << tmppreamble
569                    << "\n\\end_preamble\n";
570         }
571
572         // the options
573         if (!options.empty()) {
574                 os << "\\options " << options << '\n';
575         }
576
577         // then the text parameters
578         if (language != ignore_language)
579                 os << "\\language " << language->lang() << '\n';
580         os << "\\inputencoding " << inputenc
581            << "\n\\fontscheme " << fonts
582            << "\n\\graphics " << graphicsDriver << '\n';
583
584         if (!float_placement.empty()) {
585                 os << "\\float_placement " << float_placement << '\n';
586         }
587         os << "\\paperfontsize " << fontsize << '\n';
588
589         spacing().writeFile(os);
590
591         os << "\\papersize " << string_papersize[papersize]
592            << "\n\\use_geometry " << convert<string>(use_geometry)
593            << "\n\\use_amsmath " << use_amsmath
594            << "\n\\cite_engine " << citeenginetranslator().find(cite_engine)
595            << "\n\\use_bibtopic " << convert<string>(use_bibtopic)
596            << "\n\\paperorientation " << string_orientation[orientation]
597            << '\n';
598
599         BranchList::const_iterator it = branchlist().begin();
600         BranchList::const_iterator end = branchlist().end();
601         for (; it != end; ++it) {
602                 os << "\\branch " << it->getBranch()
603                    << "\n\\selected " << it->getSelected()
604                    << "\n\\color " << lyx::X11hexname(it->getColor())
605                    << "\n\\end_branch"
606                    << "\n";
607         }
608
609         if (!paperwidth.empty())
610                 os << "\\paperwidth "
611                    << VSpace(paperwidth).asLyXCommand() << '\n';
612         if (!paperheight.empty())
613                 os << "\\paperheight "
614                    << VSpace(paperheight).asLyXCommand() << '\n';
615         if (!leftmargin.empty())
616                 os << "\\leftmargin "
617                    << VSpace(leftmargin).asLyXCommand() << '\n';
618         if (!topmargin.empty())
619                 os << "\\topmargin "
620                    << VSpace(topmargin).asLyXCommand() << '\n';
621         if (!rightmargin.empty())
622                 os << "\\rightmargin "
623                    << VSpace(rightmargin).asLyXCommand() << '\n';
624         if (!bottommargin.empty())
625                 os << "\\bottommargin "
626                    << VSpace(bottommargin).asLyXCommand() << '\n';
627         if (!headheight.empty())
628                 os << "\\headheight "
629                    << VSpace(headheight).asLyXCommand() << '\n';
630         if (!headsep.empty())
631                 os << "\\headsep "
632                    << VSpace(headsep).asLyXCommand() << '\n';
633         if (!footskip.empty())
634                 os << "\\footskip "
635                    << VSpace(footskip).asLyXCommand() << '\n';
636         os << "\\secnumdepth " << secnumdepth
637            << "\n\\tocdepth " << tocdepth
638            << "\n\\paragraph_separation "
639            << string_paragraph_separation[paragraph_separation]
640            << "\n\\defskip " << getDefSkip().asLyXCommand()
641            << "\n\\quotes_language "
642            << string_quotes_language[quotes_language]
643            << "\n\\papercolumns " << columns
644            << "\n\\papersides " << sides
645            << "\n\\paperpagestyle " << pagestyle << '\n';
646         for (int i = 0; i < 4; ++i) {
647                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
648                         if (user_defined_bullet(i).getFont() != -1) {
649                                 os << "\\bullet " << i << " "
650                                    << user_defined_bullet(i).getFont() << " "
651                                    << user_defined_bullet(i).getCharacter() << " "
652                                    << user_defined_bullet(i).getSize() << "\n";
653                         }
654                         else {
655                                 os << "\\bulletLaTeX " << i << " \""
656                                    << user_defined_bullet(i).getText()
657                                    << "\"\n";
658                         }
659                 }
660         }
661
662         os << "\\tracking_changes " << convert<string>(tracking_changes) << "\n";
663         os << "\\output_changes " << convert<string>(output_changes) << "\n";
664
665         if (tracking_changes) {
666                 AuthorList::Authors::const_iterator it = pimpl_->authorlist.begin();
667                 AuthorList::Authors::const_iterator end = pimpl_->authorlist.end();
668                 for (; it != end; ++it) {
669                         os << "\\author " << it->second << "\n";
670                 }
671         }
672 }
673
674
675 bool BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
676                               TexRow & texrow) const
677 {
678         os << "\\documentclass";
679
680         LyXTextClass const & tclass = getLyXTextClass();
681
682         ostringstream clsoptions; // the document class options.
683
684         if (tokenPos(tclass.opt_fontsize(),
685                      '|', fontsize) >= 0) {
686                 // only write if existing in list (and not default)
687                 clsoptions << fontsize << "pt,";
688         }
689
690         // custom, A3, B3 and B4 paper sizes need geometry
691         bool nonstandard_papersize = (papersize == PAPER_B3) ||
692                                      (papersize == PAPER_B4) ||
693                                      (papersize == PAPER_A3) ||
694                                      (papersize == PAPER_CUSTOM);
695
696         if (!use_geometry) {
697                 switch (papersize) {
698                 case PAPER_A4:
699                         clsoptions << "a4paper,";
700                         break;
701                 case PAPER_USLETTER:
702                         clsoptions << "letterpaper,";
703                         break;
704                 case PAPER_A5:
705                         clsoptions << "a5paper,";
706                         break;
707                 case PAPER_B5:
708                         clsoptions << "b5paper,";
709                         break;
710                 case PAPER_USEXECUTIVE:
711                         clsoptions << "executivepaper,";
712                         break;
713                 case PAPER_USLEGAL:
714                         clsoptions << "legalpaper,";
715                         break;
716                 case PAPER_DEFAULT:
717                 case PAPER_A3:
718                 case PAPER_B3:
719                 case PAPER_B4:
720                 case PAPER_CUSTOM:
721                         break;
722                 }
723         }
724
725         // if needed
726         if (sides != tclass.sides()) {
727                 switch (sides) {
728                 case LyXTextClass::OneSide:
729                         clsoptions << "oneside,";
730                         break;
731                 case LyXTextClass::TwoSides:
732                         clsoptions << "twoside,";
733                         break;
734                 }
735         }
736
737         // if needed
738         if (columns != tclass.columns()) {
739                 if (columns == 2)
740                         clsoptions << "twocolumn,";
741                 else
742                         clsoptions << "onecolumn,";
743         }
744
745         if (!use_geometry
746             && orientation == ORIENTATION_LANDSCAPE)
747                 clsoptions << "landscape,";
748
749         // language should be a parameter to \documentclass
750         if (language->babel() == "hebrew"
751             && default_language->babel() != "hebrew")
752                 // This seems necessary
753                 features.useLanguage(default_language);
754
755         ostringstream language_options;
756         bool const use_babel = features.useBabel();
757         if (use_babel) {
758                 language_options << features.getLanguages();
759                 language_options << language->babel();
760                 if (lyxrc.language_global_options)
761                         clsoptions << language_options.str() << ',';
762         }
763
764         // the user-defined options
765         if (!options.empty()) {
766                 clsoptions << options << ',';
767         }
768
769         string strOptions(clsoptions.str());
770         if (!strOptions.empty()) {
771                 strOptions = rtrim(strOptions, ",");
772                 os << '[' << strOptions << ']';
773         }
774
775         os << '{' << tclass.latexname() << "}\n";
776         texrow.newline();
777         // end of \documentclass defs
778
779         // font selection must be done before loading fontenc.sty
780         // The ae package is not needed when using OT1 font encoding.
781         if (fonts != "default" &&
782             (fonts != "ae" || lyxrc.fontenc != "default")) {
783                 os << "\\usepackage{" << fonts << "}\n";
784                 texrow.newline();
785                 if (fonts == "ae") {
786                         os << "\\usepackage{aecompl}\n";
787                         texrow.newline();
788                 }
789         }
790         // this one is not per buffer
791         if (lyxrc.fontenc != "default") {
792                 os << "\\usepackage[" << lyxrc.fontenc
793                    << "]{fontenc}\n";
794                 texrow.newline();
795         }
796
797         if (inputenc == "auto") {
798                 string const doc_encoding =
799                         language->encoding()->LatexName();
800
801                 // Create a list with all the input encodings used
802                 // in the document
803                 std::set<string> encodings =
804                         features.getEncodingSet(doc_encoding);
805
806                 os << "\\usepackage[";
807                 std::copy(encodings.begin(), encodings.end(),
808                           std::ostream_iterator<string>(os, ","));
809                 os << doc_encoding << "]{inputenc}\n";
810                 texrow.newline();
811         } else if (inputenc != "default") {
812                 os << "\\usepackage[" << inputenc
813                    << "]{inputenc}\n";
814                 texrow.newline();
815         }
816
817         if (use_geometry || nonstandard_papersize) {
818                 os << "\\usepackage{geometry}\n";
819                 texrow.newline();
820                 os << "\\geometry{verbose";
821                 if (orientation == ORIENTATION_LANDSCAPE)
822                         os << ",landscape";
823                 switch (papersize) {
824                 case PAPER_CUSTOM:
825                         if (!paperwidth.empty())
826                                 os << ",paperwidth="
827                                    << paperwidth;
828                         if (!paperheight.empty())
829                                 os << ",paperheight="
830                                    << paperheight;
831                         break;
832                 case PAPER_USLETTER:
833                         os << ",letterpaper";
834                         break;
835                 case PAPER_USLEGAL:
836                         os << ",legalpaper";
837                         break;
838                 case PAPER_USEXECUTIVE:
839                         os << ",executivepaper";
840                         break;
841                 case PAPER_A3:
842                         os << ",a3paper";
843                         break;
844                 case PAPER_A4:
845                         os << ",a4paper";
846                         break;
847                 case PAPER_A5:
848                         os << ",a5paper";
849                         break;
850                 case PAPER_B3:
851                         os << ",b3paper";
852                         break;
853                 case PAPER_B4:
854                         os << ",b4paper";
855                         break;
856                 case PAPER_B5:
857                         os << ",b5paper";
858                         break;
859                 default:
860                         // default papersize ie PAPER_DEFAULT
861                         switch (lyxrc.default_papersize) {
862                         case PAPER_DEFAULT: // keep compiler happy
863                         case PAPER_USLETTER:
864                                 os << ",letterpaper";
865                                 break;
866                         case PAPER_USLEGAL:
867                                 os << ",legalpaper";
868                                 break;
869                         case PAPER_USEXECUTIVE:
870                                 os << ",executivepaper";
871                                 break;
872                         case PAPER_A3:
873                                 os << ",a3paper";
874                                 break;
875                         case PAPER_A4:
876                                 os << ",a4paper";
877                                 break;
878                         case PAPER_A5:
879                                 os << ",a5paper";
880                                 break;
881                         case PAPER_B5:
882                                 os << ",b5paper";
883                                 break;
884                         case PAPER_B3:
885                         case PAPER_B4:
886                         case PAPER_CUSTOM:
887                                 break;
888                         }
889                 }
890                 if (!topmargin.empty())
891                         os << ",tmargin=" << topmargin;
892                 if (!bottommargin.empty())
893                         os << ",bmargin=" << bottommargin;
894                 if (!leftmargin.empty())
895                         os << ",lmargin=" << leftmargin;
896                 if (!rightmargin.empty())
897                         os << ",rmargin=" << rightmargin;
898                 if (!headheight.empty())
899                         os << ",headheight=" << headheight;
900                 if (!headsep.empty())
901                         os << ",headsep=" << headsep;
902                 if (!footskip.empty())
903                         os << ",footskip=" << footskip;
904                 os << "}\n";
905                 texrow.newline();
906         }
907
908         if (tokenPos(tclass.opt_pagestyle(),
909                      '|', pagestyle) >= 0) {
910                 if (pagestyle == "fancy") {
911                         os << "\\usepackage{fancyhdr}\n";
912                         texrow.newline();
913                 }
914                 os << "\\pagestyle{" << pagestyle << "}\n";
915                 texrow.newline();
916         }
917
918         // Only if class has a ToC hierarchy
919         if (tclass.hasTocLevels()) {
920                 if (secnumdepth != tclass.secnumdepth()) {
921                         os << "\\setcounter{secnumdepth}{"
922                            << secnumdepth
923                            << "}\n";
924                         texrow.newline();
925                 }
926                 if (tocdepth != tclass.tocdepth()) {
927                         os << "\\setcounter{tocdepth}{"
928                            << tocdepth
929                            << "}\n";
930                         texrow.newline();
931                 }
932         }
933
934         if (paragraph_separation) {
935                 switch (getDefSkip().kind()) {
936                 case VSpace::SMALLSKIP:
937                         os << "\\setlength\\parskip{\\smallskipamount}\n";
938                         break;
939                 case VSpace::MEDSKIP:
940                         os << "\\setlength\\parskip{\\medskipamount}\n";
941                         break;
942                 case VSpace::BIGSKIP:
943                         os << "\\setlength\\parskip{\\bigskipamount}\n";
944                         break;
945                 case VSpace::LENGTH:
946                         os << "\\setlength\\parskip{"
947                            << getDefSkip().length().asLatexString()
948                            << "}\n";
949                         break;
950                 default: // should never happen // Then delete it.
951                         os << "\\setlength\\parskip{\\medskipamount}\n";
952                         break;
953                 }
954                 texrow.newline();
955
956                 os << "\\setlength\\parindent{0pt}\n";
957                 texrow.newline();
958         }
959
960         // If we use jurabib, we have to call babel here.
961         if (use_babel && features.isRequired("jurabib")) {
962                 os << babelCall(language_options.str())
963                    << '\n'
964                    << features.getBabelOptions();
965                 texrow.newline();
966         }
967
968         // Now insert the LyX specific LaTeX commands...
969
970         // The optional packages;
971         string lyxpreamble(features.getPackages());
972
973         // this might be useful...
974         lyxpreamble += "\n\\makeatletter\n";
975
976         // Some macros LyX will need
977         string tmppreamble(features.getMacros());
978
979         if (!tmppreamble.empty()) {
980                 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
981                         "LyX specific LaTeX commands.\n"
982                         + tmppreamble + '\n';
983         }
984
985         // the text class specific preamble
986         tmppreamble = features.getTClassPreamble();
987         if (!tmppreamble.empty()) {
988                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
989                         "Textclass specific LaTeX commands.\n"
990                         + tmppreamble + '\n';
991         }
992
993         /* the user-defined preamble */
994         if (!preamble.empty()) {
995                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
996                         "User specified LaTeX commands.\n"
997                         + preamble + '\n';
998         }
999
1000         // Itemize bullet settings need to be last in case the user
1001         // defines their own bullets that use a package included
1002         // in the user-defined preamble -- ARRae
1003         // Actually it has to be done much later than that
1004         // since some packages like frenchb make modifications
1005         // at \begin{document} time -- JMarc
1006         string bullets_def;
1007         for (int i = 0; i < 4; ++i) {
1008                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1009                         if (bullets_def.empty())
1010                                 bullets_def="\\AtBeginDocument{\n";
1011                         bullets_def += "  \\def\\labelitemi";
1012                         switch (i) {
1013                                 // `i' is one less than the item to modify
1014                         case 0:
1015                                 break;
1016                         case 1:
1017                                 bullets_def += 'i';
1018                                 break;
1019                         case 2:
1020                                 bullets_def += "ii";
1021                                 break;
1022                         case 3:
1023                                 bullets_def += 'v';
1024                                 break;
1025                         }
1026                         bullets_def += '{' +
1027                                 user_defined_bullet(i).getText()
1028                                 + "}\n";
1029                 }
1030         }
1031
1032         if (!bullets_def.empty())
1033                 lyxpreamble += bullets_def + "}\n\n";
1034
1035         // We try to load babel late, in case it interferes
1036         // with other packages.
1037         // Jurabib has to be called after babel, though.
1038         if (use_babel && !features.isRequired("jurabib")) {
1039                 lyxpreamble += babelCall(language_options.str()) + '\n';
1040                 lyxpreamble += features.getBabelOptions();
1041         }
1042
1043         lyxpreamble += "\\makeatother\n";
1044
1045         // dvipost settings come after everything else
1046         if (features.isAvailable("dvipost") && tracking_changes && output_changes) {
1047                 lyxpreamble +=
1048                         "\\dvipostlayout\n"
1049                         "\\dvipost{osstart color push Red}\n"
1050                         "\\dvipost{osend color pop}\n"
1051                         "\\dvipost{cbstart color push Blue}\n"
1052                         "\\dvipost{cbend color pop}\n";
1053         }
1054
1055         int const nlines =
1056                 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
1057         for (int j = 0; j != nlines; ++j) {
1058                 texrow.newline();
1059         }
1060
1061         os << lyxpreamble;
1062         return use_babel;
1063 }
1064
1065
1066 void BufferParams::useClassDefaults()
1067 {
1068         LyXTextClass const & tclass = textclasslist[textclass];
1069
1070         sides = tclass.sides();
1071         columns = tclass.columns();
1072         pagestyle = tclass.pagestyle();
1073         options = tclass.options();
1074         // Only if class has a ToC hierarchy
1075         if (tclass.hasTocLevels()) {
1076                 secnumdepth = tclass.secnumdepth();
1077                 tocdepth = tclass.tocdepth();
1078         }
1079 }
1080
1081
1082 bool BufferParams::hasClassDefaults() const
1083 {
1084         LyXTextClass const & tclass = textclasslist[textclass];
1085
1086         return (sides == tclass.sides()
1087                 && columns == tclass.columns()
1088                 && pagestyle == tclass.pagestyle()
1089                 && options == tclass.options()
1090                 && secnumdepth == tclass.secnumdepth()
1091                 && tocdepth == tclass.tocdepth());
1092 }
1093
1094
1095 LyXTextClass const & BufferParams::getLyXTextClass() const
1096 {
1097         return textclasslist[textclass];
1098 }
1099
1100
1101 LyXFont const BufferParams::getFont() const
1102 {
1103         LyXFont f = getLyXTextClass().defaultfont();
1104         f.setLanguage(language);
1105         return f;
1106 }
1107
1108
1109 void BufferParams::readPreamble(LyXLex & lex)
1110 {
1111         if (lex.getString() != "\\begin_preamble")
1112                 lyxerr << "Error (BufferParams::readPreamble):"
1113                         "consistency check failed." << endl;
1114
1115         preamble = lex.getLongString("\\end_preamble");
1116 }
1117
1118
1119 void BufferParams::readLanguage(LyXLex & lex)
1120 {
1121         if (!lex.next()) return;
1122
1123         string const tmptok = lex.getString();
1124
1125         // check if tmptok is part of tex_babel in tex-defs.h
1126         language = languages.getLanguage(tmptok);
1127         if (!language) {
1128                 // Language tmptok was not found
1129                 language = default_language;
1130                 lyxerr << "Warning: Setting language `"
1131                        << tmptok << "' to `" << language->lang()
1132                        << "'." << endl;
1133         }
1134 }
1135
1136
1137 void BufferParams::readGraphicsDriver(LyXLex & lex)
1138 {
1139         if (!lex.next()) return;
1140
1141         string const tmptok = lex.getString();
1142         // check if tmptok is part of tex_graphics in tex_defs.h
1143         int n = 0;
1144         while (true) {
1145                 string const test = tex_graphics[n++];
1146
1147                 if (test == tmptok) {
1148                         graphicsDriver = tmptok;
1149                         break;
1150                 } else if (test == "") {
1151                         lex.printError(
1152                                 "Warning: graphics driver `$$Token' not recognized!\n"
1153                                 "         Setting graphics driver to `default'.\n");
1154                         graphicsDriver = "default";
1155                         break;
1156                 }
1157         }
1158 }
1159
1160
1161 void BufferParams::readBullets(LyXLex & lex)
1162 {
1163         if (!lex.next()) return;
1164
1165         int const index = lex.getInteger();
1166         lex.next();
1167         int temp_int = lex.getInteger();
1168         user_defined_bullet(index).setFont(temp_int);
1169         temp_bullet(index).setFont(temp_int);
1170         lex >> temp_int;
1171         user_defined_bullet(index).setCharacter(temp_int);
1172         temp_bullet(index).setCharacter(temp_int);
1173         lex >> temp_int;
1174         user_defined_bullet(index).setSize(temp_int);
1175         temp_bullet(index).setSize(temp_int);
1176 }
1177
1178
1179 void BufferParams::readBulletsLaTeX(LyXLex & lex)
1180 {
1181         // The bullet class should be able to read this.
1182         if (!lex.next()) return;
1183         int const index = lex.getInteger();
1184         lex.next(true);
1185         string const temp_str = lex.getString();
1186
1187         user_defined_bullet(index).setText(temp_str);
1188         temp_bullet(index).setText(temp_str);
1189 }
1190
1191
1192 string const BufferParams::paperSizeName() const
1193 {
1194         char real_papersize = papersize;
1195         if (real_papersize == PAPER_DEFAULT)
1196                 real_papersize = lyxrc.default_papersize;
1197
1198         switch (real_papersize) {
1199         case PAPER_A3:
1200                 return "a3";
1201         case PAPER_A4:
1202                 return "a4";
1203         case PAPER_A5:
1204                 return "a5";
1205         case PAPER_B5:
1206                 return "b5";
1207         case PAPER_USEXECUTIVE:
1208                 return "foolscap";
1209         case PAPER_USLEGAL:
1210                 return "legal";
1211         case PAPER_USLETTER:
1212         default:
1213                 return "letter";
1214         }
1215 }
1216
1217
1218 string const BufferParams::dvips_options() const
1219 {
1220         string result;
1221
1222         if (use_geometry
1223             && papersize == PAPER_CUSTOM
1224             && !lyxrc.print_paper_dimension_flag.empty()
1225             && !paperwidth.empty()
1226             && !paperheight.empty()) {
1227                 // using a custom papersize
1228                 result = lyxrc.print_paper_dimension_flag;
1229                 result += ' ' + paperwidth;
1230                 result += ',' + paperheight;
1231         } else {
1232                 string const paper_option = paperSizeName();
1233                 if (paper_option != "letter" ||
1234                     orientation != ORIENTATION_LANDSCAPE) {
1235                         // dvips won't accept -t letter -t landscape.
1236                         // In all other cases, include the paper size
1237                         // explicitly.
1238                         result = lyxrc.print_paper_flag;
1239                         result += ' ' + paper_option;
1240                 }
1241         }
1242         if (orientation == ORIENTATION_LANDSCAPE &&
1243             papersize != PAPER_CUSTOM)
1244                 result += ' ' + lyxrc.print_landscape_flag;
1245         return result;
1246 }
1247
1248
1249 string const BufferParams::babelCall(string const & lang_opts) const
1250 {
1251         string tmp = lyxrc.language_package;
1252         if (!lyxrc.language_global_options && tmp == "\\usepackage{babel}")
1253                 tmp = string("\\usepackage[") + lang_opts + "]{babel}";
1254         return tmp;
1255 }