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