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