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