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