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