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