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