]> git.lyx.org Git - lyx.git/blob - src/bufferparams.C
move some selection related stuff over to textcursor.C
[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 = VMARGIN_PAPER_TYPE(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 = PAPER_PACKAGES(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_A3PAPER:
477                         clsoptions << "a3paper,";
478                         break;
479                 case PAPER_A4PAPER:
480                         clsoptions << "a4paper,";
481                         break;
482                 case PAPER_USLETTER:
483                         clsoptions << "letterpaper,";
484                         break;
485                 case PAPER_A5PAPER:
486                         clsoptions << "a5paper,";
487                         break;
488                 case PAPER_B5PAPER:
489                         clsoptions << "b5paper,";
490                         break;
491                 case PAPER_EXECUTIVEPAPER:
492                         clsoptions << "executivepaper,";
493                         break;
494                 case PAPER_LEGALPAPER:
495                         clsoptions << "legalpaper,";
496                         break;
497                 case PAPER_DEFAULT:
498                         break;
499                 }
500         }
501
502         // if needed
503         if (sides != tclass.sides()) {
504                 switch (sides) {
505                 case LyXTextClass::OneSide:
506                         clsoptions << "oneside,";
507                         break;
508                 case LyXTextClass::TwoSides:
509                         clsoptions << "twoside,";
510                         break;
511                 }
512         }
513
514         // if needed
515         if (columns != tclass.columns()) {
516                 if (columns == 2)
517                         clsoptions << "twocolumn,";
518                 else
519                         clsoptions << "onecolumn,";
520         }
521
522         if (!use_geometry
523             && orientation == ORIENTATION_LANDSCAPE)
524                 clsoptions << "landscape,";
525
526         // language should be a parameter to \documentclass
527         if (language->babel() == "hebrew"
528             && default_language->babel() != "hebrew")
529                 // This seems necessary
530                 features.useLanguage(default_language);
531
532         ostringstream language_options;
533         bool const use_babel = features.useBabel();
534         if (use_babel) {
535                 language_options << features.getLanguages();
536                 language_options << language->babel();
537                 if (lyxrc.language_global_options)
538                         clsoptions << language_options.str() << ',';
539         }
540
541         // the user-defined options
542         if (!options.empty()) {
543                 clsoptions << options << ',';
544         }
545
546         string strOptions(STRCONV(clsoptions.str()));
547         if (!strOptions.empty()) {
548                 strOptions = rtrim(strOptions, ",");
549                 os << '[' << strOptions << ']';
550         }
551
552         os << '{' << tclass.latexname() << "}\n";
553         texrow.newline();
554         // end of \documentclass defs
555
556         // font selection must be done before loading fontenc.sty
557         // The ae package is not needed when using OT1 font encoding.
558         if (fonts != "default" &&
559             (fonts != "ae" || lyxrc.fontenc != "default")) {
560                 os << "\\usepackage{" << fonts << "}\n";
561                 texrow.newline();
562                 if (fonts == "ae") {
563                         os << "\\usepackage{aecompl}\n";
564                         texrow.newline();
565                 }
566         }
567         // this one is not per buffer
568         if (lyxrc.fontenc != "default") {
569                 os << "\\usepackage[" << lyxrc.fontenc
570                    << "]{fontenc}\n";
571                 texrow.newline();
572         }
573
574         if (inputenc == "auto") {
575                 string const doc_encoding =
576                         language->encoding()->LatexName();
577
578                 // Create a list with all the input encodings used
579                 // in the document
580                 std::set<string> encodings =
581                         features.getEncodingSet(doc_encoding);
582
583                 os << "\\usepackage[";
584                 std::copy(encodings.begin(), encodings.end(),
585                           std::ostream_iterator<string>(os, ","));
586                 os << doc_encoding << "]{inputenc}\n";
587                 texrow.newline();
588         } else if (inputenc != "default") {
589                 os << "\\usepackage[" << inputenc
590                    << "]{inputenc}\n";
591                 texrow.newline();
592         }
593
594         // At the very beginning the text parameters.
595         if (paperpackage != PACKAGE_NONE) {
596                 switch (paperpackage) {
597                 case PACKAGE_NONE:
598                         break;
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         return use_babel;
849 }
850
851 void BufferParams::setPaperStuff()
852 {
853         papersize = PAPER_DEFAULT;
854         char const c1 = paperpackage;
855         if (c1 == PACKAGE_NONE) {
856                 char const c2 = papersize2;
857                 if (c2 == VM_PAPER_USLETTER)
858                         papersize = PAPER_USLETTER;
859                 else if (c2 == VM_PAPER_USLEGAL)
860                         papersize = PAPER_LEGALPAPER;
861                 else if (c2 == VM_PAPER_USEXECUTIVE)
862                         papersize = PAPER_EXECUTIVEPAPER;
863                 else if (c2 == VM_PAPER_A3)
864                         papersize = PAPER_A3PAPER;
865                 else if (c2 == VM_PAPER_A4)
866                         papersize = PAPER_A4PAPER;
867                 else if (c2 == VM_PAPER_A5)
868                         papersize = PAPER_A5PAPER;
869                 else if ((c2 == VM_PAPER_B3) || (c2 == VM_PAPER_B4) ||
870                          (c2 == VM_PAPER_B5))
871                         papersize = PAPER_B5PAPER;
872         } else if ((c1 == PACKAGE_A4) || (c1 == PACKAGE_A4WIDE) ||
873                    (c1 == PACKAGE_WIDEMARGINSA4))
874                 papersize = PAPER_A4PAPER;
875 }
876
877
878 void BufferParams::useClassDefaults()
879 {
880         LyXTextClass const & tclass = textclasslist[textclass];
881
882         sides = tclass.sides();
883         columns = tclass.columns();
884         pagestyle = tclass.pagestyle();
885         options = tclass.options();
886         secnumdepth = tclass.secnumdepth();
887         tocdepth = tclass.tocdepth();
888 }
889
890
891 bool BufferParams::hasClassDefaults() const
892 {
893         LyXTextClass const & tclass = textclasslist[textclass];
894
895         return (sides == tclass.sides()
896                 && columns == tclass.columns()
897                 && pagestyle == tclass.pagestyle()
898                 && options == tclass.options()
899                 && secnumdepth == tclass.secnumdepth()
900                 && tocdepth == tclass.tocdepth());
901 }
902
903
904 LyXTextClass const & BufferParams::getLyXTextClass() const
905 {
906         return textclasslist[textclass];
907 }
908
909
910 void BufferParams::readPreamble(LyXLex & lex)
911 {
912         if (lex.getString() != "\\begin_preamble")
913                 lyxerr << "Error (BufferParams::readPreamble):"
914                         "consistency check failed." << endl;
915
916         preamble = lex.getLongString("\\end_preamble");
917 }
918
919
920 void BufferParams::readLanguage(LyXLex & lex)
921 {
922         if (!lex.next()) return;
923
924         string const tmptok = lex.getString();
925
926         // check if tmptok is part of tex_babel in tex-defs.h
927         language = languages.getLanguage(tmptok);
928         if (!language) {
929                 // Language tmptok was not found
930                 language = default_language;
931                 lyxerr << "Warning: Setting language `"
932                        << tmptok << "' to `" << language->lang()
933                        << "'." << endl;
934         }
935 }
936
937
938 void BufferParams::readGraphicsDriver(LyXLex & lex)
939 {
940         if (!lex.next()) return;
941
942         string const tmptok = lex.getString();
943         // check if tmptok is part of tex_graphics in tex_defs.h
944         int n = 0;
945         while (true) {
946                 string const test = tex_graphics[n++];
947
948                 if (test == tmptok) {
949                         graphicsDriver = tmptok;
950                         break;
951                 } else if (test == "last_item") {
952                         lex.printError(
953                                 "Warning: graphics driver `$$Token' not recognized!\n"
954                                 "         Setting graphics driver to `default'.\n");
955                         graphicsDriver = "default";
956                         break;
957                 }
958         }
959 }