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