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