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