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