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