]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
Fix my breakage. Sorry guys.
[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, 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         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         papersize2 = VM_PAPER_DEFAULT; /* DEFAULT */
337         paperpackage = PACKAGE_NONE;
338         orientation = ORIENTATION_PORTRAIT;
339         use_geometry = false;
340         use_amsmath = AMS_AUTO;
341         cite_engine = biblio::ENGINE_BASIC;
342         use_bibtopic = false;
343         tracking_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                 papersize2 = 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 == "\\branch") {
512                 lex.next();
513                 string branch = lex.getString();
514                 branchlist().add(branch);
515                 while (true) {
516                         lex.next();
517                         string const tok = lex.getString();
518                         if (tok == "\\end_branch")
519                                 break;
520                         Branch * branch_ptr = branchlist().find(branch);
521                         if (tok == "\\selected") {
522                                 lex.next();
523                                 if (branch_ptr)
524                                         branch_ptr->setSelected(lex.getInteger());
525                         }
526                         // not yet operational
527                         if (tok == "\\color") {
528                                 lex.eatLine();
529                                 string color = lex.getString();
530                                 if (branch_ptr)
531                                         branch_ptr->setColor(color);
532                                 // Update also the LColor table:
533                                 if (color == "none")
534                                         color = lcolor.getX11Name(LColor::background);
535                                 lcolor.setColor(branch, color);
536
537                         }
538                 }
539         } else if (token == "\\author") {
540                 lex.eatLine();
541                 istringstream ss(lex.getString());
542                 Author a;
543                 ss >> a;
544                 author_map.push_back(pimpl_->authorlist.record(a));
545         } else if (token == "\\paperorientation") {
546                 string orient;
547                 lex >> orient;
548                 orientation = paperorientationtranslator().find(orient);
549         } else if (token == "\\paperwidth") {
550                 lex >> paperwidth;
551         } else if (token == "\\paperheight") {
552                 lex >> paperheight;
553         } else if (token == "\\leftmargin") {
554                 lex >> leftmargin;
555         } else if (token == "\\topmargin") {
556                 lex >> topmargin;
557         } else if (token == "\\rightmargin") {
558                 lex >> rightmargin;
559         } else if (token == "\\bottommargin") {
560                 lex >> bottommargin;
561         } else if (token == "\\headheight") {
562                 lex >> headheight;
563         } else if (token == "\\headsep") {
564                 lex >> headsep;
565         } else if (token == "\\footskip") {
566                 lex >> footskip;
567         } else if (token == "\\paperfontsize") {
568                 lex >> fontsize;
569         } else if (token == "\\papercolumns") {
570                 lex >> columns;
571         } else if (token == "\\papersides") {
572                 int psides;
573                 lex >> psides;
574                 sides = sidestranslator().find(psides);
575         } else if (token == "\\paperpagestyle") {
576                 lex >> pagestyle;
577         } else if (token == "\\bullet") {
578                 readBullets(lex);
579         } else if (token == "\\bulletLaTeX") {
580                 readBulletsLaTeX(lex);
581         } else if (token == "\\secnumdepth") {
582                 lex >> secnumdepth;
583         } else if (token == "\\tocdepth") {
584                 lex >> tocdepth;
585         } else if (token == "\\spacing") {
586                 string nspacing;
587                 lex >> nspacing;
588                 string tmp_val;
589                 if (nspacing == "other") {
590                         lex >> tmp_val;
591                 }
592                 spacing().set(spacetranslator().find(nspacing), tmp_val);
593         } else if (token == "\\float_placement") {
594                 lex >> float_placement;
595         } else {
596                 return token;
597         }
598
599         return string();
600 }
601
602
603 void BufferParams::writeFile(ostream & os) const
604 {
605         // The top of the file is written by the buffer.
606         // Prints out the buffer info into the .lyx file given by file
607
608         // the textclass
609         os << "\\textclass " << textclasslist[textclass].name() << '\n';
610
611         // then the the preamble
612         if (!preamble.empty()) {
613                 // remove '\n' from the end of preamble
614                 string const tmppreamble = rtrim(preamble, "\n");
615                 os << "\\begin_preamble\n"
616                    << tmppreamble
617                    << "\n\\end_preamble\n";
618         }
619
620         // the options
621         if (!options.empty()) {
622                 os << "\\options " << options << '\n';
623         }
624
625         // then the text parameters
626         if (language != ignore_language)
627                 os << "\\language " << language->lang() << '\n';
628         os << "\\inputencoding " << inputenc
629            << "\n\\fontscheme " << fonts
630            << "\n\\graphics " << graphicsDriver << '\n';
631
632         if (!float_placement.empty()) {
633                 os << "\\float_placement " << float_placement << '\n';
634         }
635         os << "\\paperfontsize " << fontsize << '\n';
636
637         spacing().writeFile(os);
638
639         os << "\\papersize " << string_papersize[papersize2]
640            << "\n\\paperpackage " << string_paperpackages[paperpackage]
641            << "\n\\use_geometry " << convert<string>(use_geometry)
642            << "\n\\use_amsmath " << use_amsmath
643            << "\n\\cite_engine " << citeenginetranslator().find(cite_engine)
644            << "\n\\use_bibtopic " << convert<string>(use_bibtopic)
645            << "\n\\paperorientation " << string_orientation[orientation]
646            << '\n';
647
648         std::list<Branch>::const_iterator it = branchlist().begin();
649         std::list<Branch>::const_iterator end = branchlist().end();
650         for (; it != end; ++it) {
651                 os << "\\branch " << it->getBranch()
652                    << "\n\\selected " << it->getSelected()
653                    << "\n\\color " << it->getColor()
654                    << "\n\\end_branch"
655                    << "\n";
656         }
657
658         if (!paperwidth.empty())
659                 os << "\\paperwidth "
660                    << VSpace(paperwidth).asLyXCommand() << '\n';
661         if (!paperheight.empty())
662                 os << "\\paperheight "
663                    << VSpace(paperheight).asLyXCommand() << '\n';
664         if (!leftmargin.empty())
665                 os << "\\leftmargin "
666                    << VSpace(leftmargin).asLyXCommand() << '\n';
667         if (!topmargin.empty())
668                 os << "\\topmargin "
669                    << VSpace(topmargin).asLyXCommand() << '\n';
670         if (!rightmargin.empty())
671                 os << "\\rightmargin "
672                    << VSpace(rightmargin).asLyXCommand() << '\n';
673         if (!bottommargin.empty())
674                 os << "\\bottommargin "
675                    << VSpace(bottommargin).asLyXCommand() << '\n';
676         if (!headheight.empty())
677                 os << "\\headheight "
678                    << VSpace(headheight).asLyXCommand() << '\n';
679         if (!headsep.empty())
680                 os << "\\headsep "
681                    << VSpace(headsep).asLyXCommand() << '\n';
682         if (!footskip.empty())
683                 os << "\\footskip "
684                    << VSpace(footskip).asLyXCommand() << '\n';
685         os << "\\secnumdepth " << secnumdepth
686            << "\n\\tocdepth " << tocdepth
687            << "\n\\paragraph_separation "
688            << string_paragraph_separation[paragraph_separation]
689            << "\n\\defskip " << getDefSkip().asLyXCommand()
690            << "\n\\quotes_language "
691            << string_quotes_language[quotes_language] << '\n'
692            << "\\quotes_times "
693            << quotestimestranslator().find(quotes_times)
694            << "\n\\papercolumns " << columns
695            << "\n\\papersides " << sides
696            << "\n\\paperpagestyle " << pagestyle << '\n';
697         for (int i = 0; i < 4; ++i) {
698                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
699                         if (user_defined_bullet(i).getFont() != -1) {
700                                 os << "\\bullet " << i << " "
701                                    << user_defined_bullet(i).getFont() << " "
702                                    << user_defined_bullet(i).getCharacter() << " "
703                                    << user_defined_bullet(i).getSize() << "\n";
704                         }
705                         else {
706                                 os << "\\bulletLaTeX " << i << " \""
707                                    << user_defined_bullet(i).getText()
708                                    << "\"\n";
709                         }
710                 }
711         }
712
713         os << "\\tracking_changes " << convert<string>(tracking_changes) << "\n";
714
715         if (tracking_changes) {
716                 AuthorList::Authors::const_iterator it = pimpl_->authorlist.begin();
717                 AuthorList::Authors::const_iterator end = pimpl_->authorlist.end();
718                 for (; it != end; ++it) {
719                         os << "\\author " << it->second << "\n";
720                 }
721         }
722 }
723
724
725 bool BufferParams::writeLaTeX(ostream & os, LaTeXFeatures & features,
726                               TexRow & texrow) const
727 {
728         os << "\\documentclass";
729
730         LyXTextClass const & tclass = getLyXTextClass();
731
732         ostringstream clsoptions; // the document class options.
733
734         if (tokenPos(tclass.opt_fontsize(),
735                      '|', fontsize) >= 0) {
736                 // only write if existing in list (and not default)
737                 clsoptions << fontsize << "pt,";
738         }
739
740
741         if (!use_geometry &&
742             (paperpackage == PACKAGE_NONE)) {
743                 switch (papersize) {
744                 case PAPER_A3PAPER:
745                         clsoptions << "a3paper,";
746                         break;
747                 case PAPER_A4PAPER:
748                         clsoptions << "a4paper,";
749                         break;
750                 case PAPER_USLETTER:
751                         clsoptions << "letterpaper,";
752                         break;
753                 case PAPER_A5PAPER:
754                         clsoptions << "a5paper,";
755                         break;
756                 case PAPER_B5PAPER:
757                         clsoptions << "b5paper,";
758                         break;
759                 case PAPER_EXECUTIVEPAPER:
760                         clsoptions << "executivepaper,";
761                         break;
762                 case PAPER_LEGALPAPER:
763                         clsoptions << "legalpaper,";
764                         break;
765                 case PAPER_DEFAULT:
766                         break;
767                 }
768         }
769
770         // if needed
771         if (sides != tclass.sides()) {
772                 switch (sides) {
773                 case LyXTextClass::OneSide:
774                         clsoptions << "oneside,";
775                         break;
776                 case LyXTextClass::TwoSides:
777                         clsoptions << "twoside,";
778                         break;
779                 }
780         }
781
782         // if needed
783         if (columns != tclass.columns()) {
784                 if (columns == 2)
785                         clsoptions << "twocolumn,";
786                 else
787                         clsoptions << "onecolumn,";
788         }
789
790         if (!use_geometry
791             && orientation == ORIENTATION_LANDSCAPE)
792                 clsoptions << "landscape,";
793
794         // language should be a parameter to \documentclass
795         if (language->babel() == "hebrew"
796             && default_language->babel() != "hebrew")
797                 // This seems necessary
798                 features.useLanguage(default_language);
799
800         ostringstream language_options;
801         bool const use_babel = features.useBabel();
802         if (use_babel) {
803                 language_options << features.getLanguages();
804                 language_options << language->babel();
805                 if (lyxrc.language_global_options)
806                         clsoptions << language_options.str() << ',';
807         }
808
809         // the user-defined options
810         if (!options.empty()) {
811                 clsoptions << options << ',';
812         }
813
814         string strOptions(clsoptions.str());
815         if (!strOptions.empty()) {
816                 strOptions = rtrim(strOptions, ",");
817                 os << '[' << strOptions << ']';
818         }
819
820         os << '{' << tclass.latexname() << "}\n";
821         texrow.newline();
822         // end of \documentclass defs
823
824         // font selection must be done before loading fontenc.sty
825         // The ae package is not needed when using OT1 font encoding.
826         if (fonts != "default" &&
827             (fonts != "ae" || lyxrc.fontenc != "default")) {
828                 os << "\\usepackage{" << fonts << "}\n";
829                 texrow.newline();
830                 if (fonts == "ae") {
831                         os << "\\usepackage{aecompl}\n";
832                         texrow.newline();
833                 }
834         }
835         // this one is not per buffer
836         if (lyxrc.fontenc != "default") {
837                 os << "\\usepackage[" << lyxrc.fontenc
838                    << "]{fontenc}\n";
839                 texrow.newline();
840         }
841
842         if (inputenc == "auto") {
843                 string const doc_encoding =
844                         language->encoding()->LatexName();
845
846                 // Create a list with all the input encodings used
847                 // in the document
848                 std::set<string> encodings =
849                         features.getEncodingSet(doc_encoding);
850
851                 os << "\\usepackage[";
852                 std::copy(encodings.begin(), encodings.end(),
853                           std::ostream_iterator<string>(os, ","));
854                 os << doc_encoding << "]{inputenc}\n";
855                 texrow.newline();
856         } else if (inputenc != "default") {
857                 os << "\\usepackage[" << inputenc
858                    << "]{inputenc}\n";
859                 texrow.newline();
860         }
861
862         // At the very beginning the text parameters.
863         if (paperpackage != PACKAGE_NONE) {
864                 switch (paperpackage) {
865                 case PACKAGE_NONE:
866                         break;
867                 case PACKAGE_A4:
868                         os << "\\usepackage{a4}\n";
869                         texrow.newline();
870                         break;
871                 case PACKAGE_A4WIDE:
872                         os << "\\usepackage{a4wide}\n";
873                         texrow.newline();
874                         break;
875                 case PACKAGE_WIDEMARGINSA4:
876                         os << "\\usepackage[widemargins]{a4}\n";
877                         texrow.newline();
878                         break;
879                 }
880         }
881         if (use_geometry) {
882                 os << "\\usepackage{geometry}\n";
883                 texrow.newline();
884                 os << "\\geometry{verbose";
885                 if (orientation == ORIENTATION_LANDSCAPE)
886                         os << ",landscape";
887                 switch (papersize2) {
888                 case VM_PAPER_CUSTOM:
889                         if (!paperwidth.empty())
890                                 os << ",paperwidth="
891                                    << paperwidth;
892                         if (!paperheight.empty())
893                                 os << ",paperheight="
894                                    << paperheight;
895                         break;
896                 case VM_PAPER_USLETTER:
897                         os << ",letterpaper";
898                         break;
899                 case VM_PAPER_USLEGAL:
900                         os << ",legalpaper";
901                         break;
902                 case VM_PAPER_USEXECUTIVE:
903                         os << ",executivepaper";
904                         break;
905                 case VM_PAPER_A3:
906                         os << ",a3paper";
907                         break;
908                 case VM_PAPER_A4:
909                         os << ",a4paper";
910                         break;
911                 case VM_PAPER_A5:
912                         os << ",a5paper";
913                         break;
914                 case VM_PAPER_B3:
915                         os << ",b3paper";
916                         break;
917                 case VM_PAPER_B4:
918                         os << ",b4paper";
919                         break;
920                 case VM_PAPER_B5:
921                         os << ",b5paper";
922                         break;
923                 default:
924                                 // default papersize ie VM_PAPER_DEFAULT
925                         switch (lyxrc.default_papersize) {
926                         case PAPER_DEFAULT: // keep compiler happy
927                         case PAPER_USLETTER:
928                                 os << ",letterpaper";
929                                 break;
930                         case PAPER_LEGALPAPER:
931                                 os << ",legalpaper";
932                                 break;
933                         case PAPER_EXECUTIVEPAPER:
934                                 os << ",executivepaper";
935                                 break;
936                         case PAPER_A3PAPER:
937                                 os << ",a3paper";
938                                 break;
939                         case PAPER_A4PAPER:
940                                 os << ",a4paper";
941                                 break;
942                         case PAPER_A5PAPER:
943                                 os << ",a5paper";
944                                 break;
945                         case PAPER_B5PAPER:
946                                 os << ",b5paper";
947                                 break;
948                         }
949                 }
950                 if (!topmargin.empty())
951                         os << ",tmargin=" << topmargin;
952                 if (!bottommargin.empty())
953                         os << ",bmargin=" << bottommargin;
954                 if (!leftmargin.empty())
955                         os << ",lmargin=" << leftmargin;
956                 if (!rightmargin.empty())
957                         os << ",rmargin=" << rightmargin;
958                 if (!headheight.empty())
959                         os << ",headheight=" << headheight;
960                 if (!headsep.empty())
961                         os << ",headsep=" << headsep;
962                 if (!footskip.empty())
963                         os << ",footskip=" << footskip;
964                 os << "}\n";
965                 texrow.newline();
966         }
967
968         if (tokenPos(tclass.opt_pagestyle(),
969                      '|', pagestyle) >= 0) {
970                 if (pagestyle == "fancy") {
971                         os << "\\usepackage{fancyhdr}\n";
972                         texrow.newline();
973                 }
974                 os << "\\pagestyle{" << pagestyle << "}\n";
975                 texrow.newline();
976         }
977
978         if (secnumdepth != tclass.secnumdepth()) {
979                 os << "\\setcounter{secnumdepth}{"
980                    << secnumdepth
981                    << "}\n";
982                 texrow.newline();
983         }
984         if (tocdepth != tclass.tocdepth()) {
985                 os << "\\setcounter{tocdepth}{"
986                    << tocdepth
987                    << "}\n";
988                 texrow.newline();
989         }
990
991         if (paragraph_separation) {
992                 switch (getDefSkip().kind()) {
993                 case VSpace::SMALLSKIP:
994                         os << "\\setlength\\parskip{\\smallskipamount}\n";
995                         break;
996                 case VSpace::MEDSKIP:
997                         os << "\\setlength\\parskip{\\medskipamount}\n";
998                         break;
999                 case VSpace::BIGSKIP:
1000                         os << "\\setlength\\parskip{\\bigskipamount}\n";
1001                         break;
1002                 case VSpace::LENGTH:
1003                         os << "\\setlength\\parskip{"
1004                            << getDefSkip().length().asLatexString()
1005                            << "}\n";
1006                         break;
1007                 default: // should never happen // Then delete it.
1008                         os << "\\setlength\\parskip{\\medskipamount}\n";
1009                         break;
1010                 }
1011                 texrow.newline();
1012
1013                 os << "\\setlength\\parindent{0pt}\n";
1014                 texrow.newline();
1015         }
1016
1017         // If we use jurabib, we have to call babel here.
1018         if (use_babel && features.isRequired("jurabib")) {
1019                 os << babelCall(language_options.str())
1020                    << '\n'
1021                    << features.getBabelOptions();
1022                 texrow.newline();
1023         }
1024
1025         // Now insert the LyX specific LaTeX commands...
1026
1027         // The optional packages;
1028         string lyxpreamble(features.getPackages());
1029
1030         // this might be useful...
1031         lyxpreamble += "\n\\makeatletter\n";
1032
1033         // Some macros LyX will need
1034         string tmppreamble(features.getMacros());
1035
1036         if (!tmppreamble.empty()) {
1037                 lyxpreamble += "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1038                         "LyX specific LaTeX commands.\n"
1039                         + tmppreamble + '\n';
1040         }
1041
1042         // the text class specific preamble
1043         tmppreamble = features.getTClassPreamble();
1044         if (!tmppreamble.empty()) {
1045                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1046                         "Textclass specific LaTeX commands.\n"
1047                         + tmppreamble + '\n';
1048         }
1049
1050         /* the user-defined preamble */
1051         if (!preamble.empty()) {
1052                 lyxpreamble += "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% "
1053                         "User specified LaTeX commands.\n"
1054                         + preamble + '\n';
1055         }
1056
1057         // Itemize bullet settings need to be last in case the user
1058         // defines their own bullets that use a package included
1059         // in the user-defined preamble -- ARRae
1060         // Actually it has to be done much later than that
1061         // since some packages like frenchb make modifications
1062         // at \begin{document} time -- JMarc
1063         string bullets_def;
1064         for (int i = 0; i < 4; ++i) {
1065                 if (user_defined_bullet(i) != ITEMIZE_DEFAULTS[i]) {
1066                         if (bullets_def.empty())
1067                                 bullets_def="\\AtBeginDocument{\n";
1068                         bullets_def += "  \\renewcommand{\\labelitemi";
1069                         switch (i) {
1070                                 // `i' is one less than the item to modify
1071                         case 0:
1072                                 break;
1073                         case 1:
1074                                 bullets_def += 'i';
1075                                 break;
1076                         case 2:
1077                                 bullets_def += "ii";
1078                                 break;
1079                         case 3:
1080                                 bullets_def += 'v';
1081                                 break;
1082                         }
1083                         bullets_def += "}{" +
1084                                 user_defined_bullet(i).getText()
1085                                 + "}\n";
1086                 }
1087         }
1088
1089         if (!bullets_def.empty())
1090                 lyxpreamble += bullets_def + "}\n\n";
1091
1092         // We try to load babel late, in case it interferes
1093         // with other packages.
1094         // Jurabib has to be called after babel, though.
1095         if (use_babel && !features.isRequired("jurabib")) {
1096                 lyxpreamble += babelCall(language_options.str()) + '\n';
1097                 lyxpreamble += features.getBabelOptions();
1098         }
1099
1100         lyxpreamble += "\\makeatother\n";
1101
1102         // dvipost settings come after everything else
1103         if (tracking_changes) {
1104                 lyxpreamble +=
1105                         "\\dvipostlayout\n"
1106                         "\\dvipost{osstart color push Red}\n"
1107                         "\\dvipost{osend color pop}\n"
1108                         "\\dvipost{cbstart color push Blue}\n"
1109                         "\\dvipost{cbend color pop}\n";
1110         }
1111
1112         int const nlines =
1113                 int(lyx::count(lyxpreamble.begin(), lyxpreamble.end(), '\n'));
1114         for (int j = 0; j != nlines; ++j) {
1115                 texrow.newline();
1116         }
1117
1118         os << lyxpreamble;
1119         return use_babel;
1120 }
1121
1122
1123 void BufferParams::setPaperStuff()
1124 {
1125         papersize = PAPER_DEFAULT;
1126         char const c1 = paperpackage;
1127         if (c1 == PACKAGE_NONE) {
1128                 char const c2 = papersize2;
1129                 if (c2 == VM_PAPER_USLETTER)
1130                         papersize = PAPER_USLETTER;
1131                 else if (c2 == VM_PAPER_USLEGAL)
1132                         papersize = PAPER_LEGALPAPER;
1133                 else if (c2 == VM_PAPER_USEXECUTIVE)
1134                         papersize = PAPER_EXECUTIVEPAPER;
1135                 else if (c2 == VM_PAPER_A3)
1136                         papersize = PAPER_A3PAPER;
1137                 else if (c2 == VM_PAPER_A4)
1138                         papersize = PAPER_A4PAPER;
1139                 else if (c2 == VM_PAPER_A5)
1140                         papersize = PAPER_A5PAPER;
1141                 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
1142                          (c2 == VM_PAPER_B5))
1143                         papersize = PAPER_B5PAPER;
1144         } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
1145                    (c1 == PACKAGE_WIDEMARGINSA4))
1146                 papersize = PAPER_A4PAPER;
1147 }
1148
1149
1150 void BufferParams::useClassDefaults()
1151 {
1152         LyXTextClass const & tclass = textclasslist[textclass];
1153
1154         sides = tclass.sides();
1155         columns = tclass.columns();
1156         pagestyle = tclass.pagestyle();
1157         options = tclass.options();
1158         secnumdepth = tclass.secnumdepth();
1159         tocdepth = tclass.tocdepth();
1160 }
1161
1162
1163 bool BufferParams::hasClassDefaults() const
1164 {
1165         LyXTextClass const & tclass = textclasslist[textclass];
1166
1167         return (sides == tclass.sides()
1168                 && columns == tclass.columns()
1169                 && pagestyle == tclass.pagestyle()
1170                 && options == tclass.options()
1171                 && secnumdepth == tclass.secnumdepth()
1172                 && tocdepth == tclass.tocdepth());
1173 }
1174
1175
1176 LyXTextClass const & BufferParams::getLyXTextClass() const
1177 {
1178         return textclasslist[textclass];
1179 }
1180
1181
1182 void BufferParams::readPreamble(LyXLex & lex)
1183 {
1184         if (lex.getString() != "\\begin_preamble")
1185                 lyxerr << "Error (BufferParams::readPreamble):"
1186                         "consistency check failed." << endl;
1187
1188         preamble = lex.getLongString("\\end_preamble");
1189 }
1190
1191
1192 void BufferParams::readLanguage(LyXLex & lex)
1193 {
1194         if (!lex.next()) return;
1195
1196         string const tmptok = lex.getString();
1197
1198         // check if tmptok is part of tex_babel in tex-defs.h
1199         language = languages.getLanguage(tmptok);
1200         if (!language) {
1201                 // Language tmptok was not found
1202                 language = default_language;
1203                 lyxerr << "Warning: Setting language `"
1204                        << tmptok << "' to `" << language->lang()
1205                        << "'." << endl;
1206         }
1207 }
1208
1209
1210 void BufferParams::readGraphicsDriver(LyXLex & lex)
1211 {
1212         if (!lex.next()) return;
1213
1214         string const tmptok = lex.getString();
1215         // check if tmptok is part of tex_graphics in tex_defs.h
1216         int n = 0;
1217         while (true) {
1218                 string const test = tex_graphics[n++];
1219
1220                 if (test == tmptok) {
1221                         graphicsDriver = tmptok;
1222                         break;
1223                 } else if (test == "last_item") {
1224                         lex.printError(
1225                                 "Warning: graphics driver `$$Token' not recognized!\n"
1226                                 "         Setting graphics driver to `default'.\n");
1227                         graphicsDriver = "default";
1228                         break;
1229                 }
1230         }
1231 }
1232
1233
1234 void BufferParams::readBullets(LyXLex & lex)
1235 {
1236         if (!lex.next()) return;
1237
1238         int const index = lex.getInteger();
1239         lex.next();
1240         int temp_int = lex.getInteger();
1241         user_defined_bullet(index).setFont(temp_int);
1242         temp_bullet(index).setFont(temp_int);
1243         lex >> temp_int;
1244         user_defined_bullet(index).setCharacter(temp_int);
1245         temp_bullet(index).setCharacter(temp_int);
1246         lex >> temp_int;
1247         user_defined_bullet(index).setSize(temp_int);
1248         temp_bullet(index).setSize(temp_int);
1249 }
1250
1251
1252 void BufferParams::readBulletsLaTeX(LyXLex & lex)
1253 {
1254         // The bullet class should be able to read this.
1255         if (!lex.next()) return;
1256         int const index = lex.getInteger();
1257         lex.next(true);
1258         string const temp_str = lex.getString();
1259
1260         user_defined_bullet(index).setText(temp_str);
1261         temp_bullet(index).setText(temp_str);
1262 }
1263
1264
1265 string const BufferParams::paperSizeName() const
1266 {
1267         char real_papersize = papersize;
1268         if (real_papersize == PAPER_DEFAULT)
1269                 real_papersize = lyxrc.default_papersize;
1270
1271         switch (real_papersize) {
1272         case PAPER_A3PAPER:
1273                 return "a3";
1274         case PAPER_A4PAPER:
1275                 return "a4";
1276         case PAPER_A5PAPER:
1277                 return "a5";
1278         case PAPER_B5PAPER:
1279                 return "b5";
1280         case PAPER_EXECUTIVEPAPER:
1281                 return "foolscap";
1282         case PAPER_LEGALPAPER:
1283                 return "legal";
1284         case PAPER_USLETTER:
1285         default:
1286                 return "letter";
1287         }
1288 }
1289
1290
1291 string const BufferParams::dvips_options() const
1292 {
1293         string result;
1294
1295         if (use_geometry
1296             && papersize2 == VM_PAPER_CUSTOM
1297             && !lyxrc.print_paper_dimension_flag.empty()
1298             && !paperwidth.empty()
1299             && !paperheight.empty()) {
1300                 // using a custom papersize
1301                 result = lyxrc.print_paper_dimension_flag;
1302                 result += ' ' + paperwidth;
1303                 result += ',' + paperheight;
1304         } else {
1305                 string const paper_option = paperSizeName();
1306                 if (paper_option != "letter" ||
1307                     orientation != ORIENTATION_LANDSCAPE) {
1308                         // dvips won't accept -t letter -t landscape.
1309                         // In all other cases, include the paper size
1310                         // explicitly.
1311                         result = lyxrc.print_paper_flag;
1312                         result += ' ' + paper_option;
1313                 }
1314         }
1315         if (orientation == ORIENTATION_LANDSCAPE &&
1316             papersize2 != VM_PAPER_CUSTOM)
1317                 result += ' ' + lyxrc.print_landscape_flag;
1318         return result;
1319 }
1320
1321
1322 string const BufferParams::babelCall(string const & lang_opts) const
1323 {
1324         string tmp = lyxrc.language_package;
1325         if (!lyxrc.language_global_options && tmp == "\\usepackage{babel}")
1326                 tmp = string("\\usepackage[") + lang_opts + "]{babel}";
1327         return tmp;
1328 }