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