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