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