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