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