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