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