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