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