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