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