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